【发布时间】:2021-09-13 13:25:13
【问题描述】:
我正在尝试使用 lxml 和 Python 2.7 解析 Visual Studio 项目文件。但是,无论我做什么,我都无法让 xpath() 函数返回除空列表之外的任何内容。我什至在调用 xpath() 之前打印了我的 etree,以确保 etree 中的所有内容看起来都很好。
这是我尝试过的众多 xpath 路径之一的示例
v = self.tree.xpath('/Project/ItemDefinitionGroup[1]/Link/LinkerScript')
这里是 Visual Studio 项目文件的 sn-p:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|VisualGDB">
<Configuration>Debug</Configuration>
<Platform>VisualGDB</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|VisualGDB">
<Configuration>Release</Configuration>
<Platform>VisualGDB</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<ProjectGuid>{52B4E371-970C-43AA-AE3C-3D3C44EB7627}</ProjectGuid>
<BSP_ID>com.sysprogs.arm.stm32</BSP_ID>
<BSP_VERSION>2021.02</BSP_VERSION>
<InPlaceBSPSubdir />
<RelativeBSPPath />
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Debug|VisualGDB'">
</PropertyGroup>
<PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Release|VisualGDB'">
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|VisualGDB'">
<GNUConfigurationType>Debug</GNUConfigurationType>
<ToolchainID>e368e833-a86e-4937-91b5-de07ceafe604</ToolchainID>
<ToolchainVersion>10.3.1/(GNU/r0</ToolchainVersion>
<MCUPropertyListFile>$(ProjectDir)stm32.props</MCUPropertyListFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|VisualGDB'">
<ToolchainID>e368e833-a86e-4937-91b5-de07ceafe604</ToolchainID>
<ToolchainVersion>10.3.1/(GNU/r0</ToolchainVersion>
<MCUPropertyListFile>$(ProjectDir)stm32.props</MCUPropertyListFile>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|VisualGDB'">
<ClCompile>
<AdditionalIncludeDirectories></AdditionalIncludeDirectories>
<PreprocessorDefinitions></PreprocessorDefinitions>
<AdditionalOptions>-fms-extensions</AdditionalOptions>
<CLanguageStandard>C11</CLanguageStandard>
<CPPLanguageStandard />
<ForcedIncludeFiles>..\Source\Assert\Assert.h;%(ForcedIncludeFiles)</ForcedIncludeFiles>
<CharSign>Unsigned</CharSign>
</ClCompile>
<Link>
<LibrarySearchDirectories>%(Link.LibrarySearchDirectories)</LibrarySearchDirectories>
<AdditionalLibraryNames>%(Link.AdditionalLibraryNames)</AdditionalLibraryNames>
<AdditionalLinkerInputs>%(Link.AdditionalLinkerInputs)</AdditionalLinkerInputs>
<AdditionalOptions>-specs=nano.specs -specs=nosys.specs -lc -lm</AdditionalOptions>
<GenerateMapFile>true</GenerateMapFile>
<MapFileName>Project.map</MapFileName>
<LinkerScript>STM32F437VI_flash.lds</LinkerScript>
</Link>
</ItemDefinitionGroup>
</Project>
文件还有更多内容,但倒数第二行 <LinkerScript>STM32F437VI_flash.lds</LinkerScript> 中的文件名是我想要得到的。
我尝试过创建自己的路径以及从以下位置获取一些生成的路径:Online xpath Ganerator
我尝试了我能想到的最简单的 xpath,但 xpath() 仍然只返回一个空列表。有人知道会发生什么吗?
【问题讨论】:
-
您需要考虑 XML 命名空间(由
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"定义)。见stackoverflow.com/a/8056239/407651 -
首先上传一个 VALID xml - 当前不是。
-
@balderman 对不起!我在最后扔了一个
</ItemDefinitionGroup>和一个</Project>,应该使它有效。 -
Scott - 我设法修复它并提取所需的信息。看我的回答。
-
谢谢! @mzjn 我肯定会更多地研究命名空间。我想弄清楚将来如何用 xpath() 做事。
标签: python xml python-2.7 parsing xpath