我有以下 xml(实际上是 .vcxproj 的一部分):
<?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 的每个数据。我可以得到元素,但不是一个特定的元素,然后验证他的值。
这是我的实际代码:
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,仅针对 Condition="'$(Configuration)|$(Platform)'=='Release 的 ?ItemDefinitionGroup |Win32'"
感谢您的帮助。
请您参考如下方法:
您还需要为其他节点使用命名空间,即 /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']
