XPath查询不输出任何结果

时间:2012-11-06 20:52:59

标签: xml dom xpath language-agnostic

为什么我不使用下面的XML和XPath代码获得任何结果?

这实际上是PowerShell,但我也用this online tool尝试了它,但又一次:它没有输出任何内容!

我想我完全忽略了什么!

[xml] $xml = @"
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup Label="ProjectConfigurations">
    <ProjectConfiguration Include="Debug|AnyCPU">
      <Configuration>Debug</Configuration>
      <Platform>AnyCPU</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Debug|ARM">
      <Configuration>Debug</Configuration>
      <Platform>ARM</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Debug|x64">
      <Configuration>Debug</Configuration>
      <Platform>x64</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Debug|x86">
      <Configuration>Debug</Configuration>
      <Platform>x86</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|AnyCPU">
      <Configuration>Release</Configuration>
      <Platform>AnyCPU</Platform>
    </ProjectConfiguration>
  </ItemGroup>
</Project>
"@;

Select-Xml "//ItemGroup" $xml | % {
    Write-Host $_.Node.GetType();
}

2 个答案:

答案 0 :(得分:1)

这是因为默认命名空间。

尝试//*[local-name()='ItemGroup']

/*/*[local-name()='ItemGroup']

/*/*

所有这3个应该返回ItemGroup

答案 1 :(得分:0)

如果你的命名空间有前缀,你也可以这样做:

//prefix:ItemGroup