是否有可能在XPath的EX pression指定只有一次的命名空间preFIX?有可能、空间、XPath、EX

2023-09-06 19:16:14 作者:我的心已经成了你的殖民地

我在XML文件中,根元素上定义只有一个命名空间。

I have only one namespace in my XML file, defined on the root element.

<root xmlns="http://my-namespace">
    <aChild>
        <secondChild />
    ...

是否有可能避免我的XPath EX pression每个元素之前写它? 后做

xmlNamespaceManager.AddNamespace("ns", "http://my-namespace");

我有写

xml.SelectNodes("ns:root/ns:aChild/ns:secondChild", xmlNamespaceManager);

那将是更容易做到这

And it would be much easier to do just

xml.SelectNodes("ns:root/aChild/secondChild", xmlNamespaceManager);

我有谁送的XPath EX pressions到XML developpers,但他们不一定知道preFIX或命名空间,他们只知道自己要访问根/ aChild / secondChild

感谢你。

推荐答案

不幸的是直观,因为它似乎,有没有简单的方法做定义的XPath默认的命名空间。没有命名空间声明的XPath名称绑定到命名空间,而不是URI分配到默认名称空间。

Unfortunately as intuitive as it seems, there's no easy way do define the default namespace in XPath. An XPath name without a namespace declaration binds to the null namespace, rather than the URI assigned to the default namespace.

这是在 XPath的默认命名空间处理和的容易的事情应该很容易