使用特定名称空间前缀的所有节点的 XPath 查询

XPath query for all nodes which use a specific namespace prefix

我有以下 XAML(它可能看起来很奇怪,因为我试图删除多余的部分):

<Root xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:a="clr-namespace:System.Collections.Generic;assembly=mscorlib"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Parent Name="SignIn">
        <a:List x:TypeArguments="InArgument(x:String)" Capacity="4" />
    </Parent>
    <a:Dictionary x:TypeArguments="x:Stringa, x:Object">
      <x:Boolean x:Key="IsExpanded">False</x:Boolean>
    </a:Dictionary>
</Root>

我需要一个 XPath 查询(限于 1.0 版,因为 .NET 项目),returns 所有 节点(不管节点的本地名称或位置如何)正在使用 a: 命名空间前缀。

In the previous example the result should contain the nodes:

/Root/Parent/a:List

/Root/a:Dictionary

我找到了如何搜索带有 specific 前缀的 specific 节点的答案,但绝对没有要搜索的东西 任何 具有特定前缀的节点。

我能找到的最接近的是 ,但它与命名空间前缀无关。

如果你想要命名空间clr-namespace:System.Collections.Generic;assembly=mscorlib中的所有元素,你可以写

//*[namespace-uri() = 'clr-namespace:System.Collections.Generic;assembly=mscorlib']

如果你真的想在前缀上搜索(大多数人会说这是不好的做法),那么你可以这样做

//*[starts-with(name(), 'a:')]

这可能适用于大多数处理器,即使 XPath 1.0 不保证它(它说 "Typically, this [the result of name()] will be the QName that occurred in the XML source. This need not be the case if there are namespace declarations in effect on the node that associate multiple prefixes with the same namespace."