2016-05-24 1 views
2

Я следующий XML (часть .vcxproj на самом деле):LXML: Как проверить значение тега в различных узлах

<?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> 

И я хочу, чтобы проверить каждый данные ClCompile или Link. Я могу получить Element, но не конкретный, и проверить его значение после.

Вот мой фактический код:

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) 

И у меня есть следующий вывод:

<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 

Теперь я хочу, чтобы проверить, если Optimization является MaxSpeed и сделать что-то после. Но я не могу. Если я попробую:

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

Он возвращает пустой список.

Как я могу просто проверить специально Optimization, только ItemDefinitionGroup с Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"?

Спасибо за помощь.

+0

try [содержит (@Condition, '$ (Конфигурация) | $ (Платформа) \' == \ 'Release | Win32' ')] –

ответ

1

Вы должны использовать пространство имен для других узлов также т.е. /ns:ClCompile/ns:Optimization, используя ваши данные выборки мы получаем:

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' 

Если вы хотите, чтобы фильтровать по MaxSpeed, вы изменили бы, чтобы:

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