2016-05-24 2 views
2

Ich habe die folgende XML (ein Teil von .vcxproj in der Tat), um zu überprüfen:lxml: Wie Tag-Wert in verschiedenen Knoten

<?xml version="1.0" encoding="utf-8"?> 
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> 
    <ClCompile> 
     <PrecompiledHeader>Create</PrecompiledHeader> 
     <WarningLevel>Level4</WarningLevel> 
     <Optimization>Disabled</Optimization> 
     <PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;ELEC_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions> 
     <RuntimeTypeInfo>false</RuntimeTypeInfo> 
     <AdditionalIncludeDirectories> 
     </AdditionalIncludeDirectories> 
     <EnableEnhancedInstructionSet>AdvancedVectorExtensions</EnableEnhancedInstructionSet> 
    </ClCompile> 
    <Link> 
     <SubSystem>Windows</SubSystem> 
     <GenerateDebugInformation>true</GenerateDebugInformation> 
    </Link> 
    </ItemDefinitionGroup> 
    <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> 
    <ClCompile> 
     <WarningLevel>Level4</WarningLevel> 
     <PrecompiledHeader>Create</PrecompiledHeader> 
     <Optimization>MaxSpeed</Optimization> 
     <FunctionLevelLinking>true</FunctionLevelLinking> 
     <IntrinsicFunctions>true</IntrinsicFunctions> 
     <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;ELEC_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions> 
     <CompileAsManaged> 
     </CompileAsManaged> 
     <RuntimeTypeInfo>false</RuntimeTypeInfo> 
     <AdditionalIncludeDirectories> 
     </AdditionalIncludeDirectories> 
     <EnableEnhancedInstructionSet>StreamingSIMDExtensions2</EnableEnhancedInstructionSet> 
    </ClCompile> 
    <Link> 
     <SubSystem>Windows</SubSystem> 
     <GenerateDebugInformation>true</GenerateDebugInformation> 
     <EnableCOMDATFolding>true</EnableCOMDATFolding> 
     <OptimizeReferences>true</OptimizeReferences> 
    </Link> 
    </ItemDefinitionGroup> 

Und ich möchte, um alle Daten von ClCompile oder Link überprüfen. Ich kann Element bekommen, aber kein spezifisches und verifiziere seinen Wert danach.

Hier ist meine eigentliche Code:

tree = etree.parse(xml) 
ns = {'ns': 'http://schemas.microsoft.com/developer/msbuild/2003'} 

debug = tree.xpath('//ns:ItemDefinitionGroup[@Condition="\'$(Configuration)|$(Platform)\'==\'Release|x64\'"]', namespaces=ns) 
for d in debug: 
    print(d) 
    for g in d: 
     print(g) 

Und ich habe die folgende Ausgabe:

<Element {http://schemas.microsoft.com/developer/msbuild/2003}ClCompile at 0x7f95a2eb2548> 
Level4 
Create 
MaxSpeed 
true 
true 
WIN32;NDEBUG;_WINDOWS;_USRDLL;ELEC_EXPORTS;%(PreprocessorDefinitions) 


false 


StreamingSIMDExtensions2 
<Element {http://schemas.microsoft.com/developer/msbuild/2003}Link at 0x7f95a2eb25c8> 
Windows 
true 
true 
true 
No dependencies 

Jetzt möchte ich überprüfen, ob OptimizationMaxSpeed ist und nach etwas tun. Aber ich kann nicht. Wenn ich es versuche:

tree.xpath('//ns:ItemDefinitionGroup[@Condition="\'$(Configuration)|$(Platform)\'==\'Release|x64\'"]/ClCompile/Optimization', namespaces=ns) 

Es gibt eine leere Liste zurück.

Wie kann ich nur speziell überprüfen Optimization, für nur ItemDefinitionGroup mit ?

Vielen Dank für Hilfe.

+0

try [contains (@Condition, '$ (Konfiguration) | $ (Platform) \' == \ Release | Win32 '')] –

Antwort

1

Sie müssen den Namespace für die anderen Knoten auch /ns:ClCompile/ns:Optimization dh verwenden, Ihre Beispieldaten erhalten wir:

In [6]: import lxml.etree as et 
In [7]: tree= et.parse("test.xml") 

In [8]: ns = {'ns': 'http://schemas.microsoft.com/developer/msbuild/2003'} 

In [9]: opts = tree.xpath("""//ns:ItemDefinitionGroup[@Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"]/ns:ClCompile/ns:Optimization""", namespaces=ns) 


In [10]: opts 
Out[10]: [<Element {http://schemas.microsoft.com/developer/msbuild/2003}Optimization at 0x7f3849c0cb00>] 

In [11]: opts[0].text 
Out[11]: 'MaxSpeed' 

Wenn Sie mit dem MaxSpeed ​​auch filtern wollten, würden Sie ändern:

/ns:ClCompile/ns:Optimization[text()='MaxSpeed']