识别内部 XML 元素的名称空间
假设我的 XML 文档中有这样的元素:
<xs:appinfo>
<CustomXML>
<Something>something</Something>
</CustomXML>
</xs:appinfo>
“xs”被声明为默认模式命名空间。我的问题是:解析器如何解释 xs:appinfo 的内部元素?它们属于哪个命名空间?
我问这个问题是因为我正在解析 C# 中的代码,并且它不断向 CustomXML 元素添加“xmlns=”“”,这让我假设否则它会将这些元素视为架构元素。
Suppose I have such elements in my XML document:
<xs:appinfo>
<CustomXML>
<Something>something</Something>
</CustomXML>
</xs:appinfo>
"xs" is declared as the default schema namespace. My question is: how would a parser interpret the inner elements of xs:appinfo? In which namespace do they belong?
I ask because I'm parsing the code in C# and it keeps adding "xmlns="" " to the CustomXML element, which makes me assume that otherwise it'd treat these elements as schema elements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 §6.2 XML 1.0 中命名空间的命名空间默认设置(第三版):
这意味着没有命名空间前缀的元素被解释为位于默认命名空间中。默认命名空间通常在文档的第一个元素上定义,如下所示:
库会在必要时(即,当您向没有命名空间的文档添加元素时)重新定义默认命名空间。换句话说,此类元素不在默认命名空间中,因此库通过向该元素添加
xmlns=""
来解决此问题,这会将此元素及其所有后代的默认命名空间重新定义为“no命名空间”。如果要添加默认命名空间中的元素,则必须显式指定它。例如,在 LINQ to XML 中:
According to §6.2 Namespace Defaulting of Namespaces in XML 1.0 (Third Edition):
That means that elements with no namespace prefix are interpreted as being in the default namespace. Default namespace is usually defined on the first element of the document and look like this:
The library redefines the default namespace when it's necessary, that is, when you add an element to the document with no namespace. In other words, such element is not in the default namespace, so the library solves this by adding
xmlns=""
to that element, which redefined the default namespace for this element and all its descendants to "no namespace".If you want to add element that is in the default namespace, you have to specify it explicitly. For example, in LINQ to XML: