Can you reuse the same namespace in an XML document

做~自己de王妃 提交于 2019-12-24 08:01:09

问题


Is it valid to have an XML document that uses the same namespace prefix twice?

In the following document the s prefix is used twice, but in the nested element the URI is different.

Is it valid?

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark">
    <fx:Declarations>
        <s:MyObject xmlns:s="library://ns.mysite.com">
            <s:Paragraph>hello world</s:Paragraph>
        </s:MyObject>
    </fx:Declarations>
</s:Application>

回答1:


Yes, prefix has no particular meaning by itself - only as alias to namespace.

Perfectly valid XML with each node name having unique namespace even if all have same prefix:

<a:r xmlns:a="urn:one">
   <a:p xmlns:a="urn:two">
        <a:c xmlns:a="urn:three">
        </a:c>
   </a:p>
</a:r>

It also applies to empty (default) prefix which can be changed for each node too. Following is valid XML with each node named with its own namespce even none have prefix:

<r xmlns="urn:one">
   <p xmlns="urn:two">
        <c xmlns="urn:three">
        </c>
   </p>
</r>

Note that prefixes in XPath selectors don't relate in to prefixes in any given XML document and must have separate mapping of prefix to namespace. Also in XPath prefixes must have unique mapping (there is no standard for it and mapping is specific for each Xml DOM implementation).

For example for first XML in this post you can't have XPath that look similar to XML like "/a:r/a:p/a:c" as each prefix must map to different namespace. Xpath would look like "/p1:r/p2:p/p3:c" with mapping of prefixes {p1->urn:one, p2->urn:two, p3->urn:three}.



来源:https://stackoverflow.com/questions/41774652/can-you-reuse-the-same-namespace-in-an-xml-document

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!