为什么我必须将空名称空间传递给XPathNavigator.GetAttribute?(Why do I have to pass an empty namespace to XPathNavigat

编程入门 行业动态 更新时间:2024-10-25 20:21:37
为什么我必须将空名称空间传递给XPathNavigator.GetAttribute?(Why do I have to pass an empty namespace to XPathNavigator.GetAttribute?)

给定以下XML标记:

<root xmlns="Demo"> <child name="foo"/> </root>

和一个位于<child>元素上的XPathNavigator ,

string withNs = navigator.GetAttribute("name", navigator.NamespaceURI); string withoutNs = navigator.GetAttribute("name", "");

产生奇怪的结果: withNs是空的,没有withoutNs包含foo 。

这是为什么? 我原以为它会反过来,因为name属性必须在Demo命名空间中,就像child元素一样。

MSDN文档没有提到传递namespaceURI=""任何神奇含义,所以我假设您必须传递属性的真实名称空间URI。

Given the following XML markup:

<root xmlns="Demo"> <child name="foo"/> </root>

and an XPathNavigator positioned on the <child> element,

string withNs = navigator.GetAttribute("name", navigator.NamespaceURI); string withoutNs = navigator.GetAttribute("name", "");

produce strange results: withNs is empty, withoutNs contains foo.

Why is that? I would have expected it would be the other way round, as the name attribute must be in the Demo namespace like the child element.

The MSDN documentation does not mention any magic meaning of passing namespaceURI="", so I assumed you have to pass the real namespace URI of the attribute.

最满意答案

因为name属性必须在Demo命名空间中,就像child元素一样。

根据w3c规范,属性不会继承它们所属元素的命名空间,这就是为什么你得到那些正确的结果的原因。

相关文章: http : //web.archive.org/web/20170118162309/http : //www.xmlplease.com/attributexmlns

as the name attribute must be in the Demo namespace like the child element.

Attributes do not inherit the namespace of the element to which they belong, as per the w3c specification, and that is why you got those results, which are correct.

Related article: http://web.archive.org/web/20170118162309/http://www.xmlplease.com/attributexmlns

更多推荐

本文发布于:2023-08-07 11:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464281.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:名称   空间   XPathNavigator   GetAttribute   empty

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!