可以在 XML 文档中重用相同的命名空间吗 [英] Can you reuse the same namespace in an XML document

查看:34
本文介绍了可以在 XML 文档中重用相同的命名空间吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个 XML 文档两次使用相同的命名空间前缀是否有效?

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

在下面的文档中,s 前缀被使用了两次,但在嵌套元素中,URI 是不同的.

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

有效吗?

<?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>

推荐答案

是的,前缀本身没有特别的意义——只是作为命名空间的别名.

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

完全有效的 XML,每个节点名称都具有唯一的命名空间,即使它们都具有相同的前缀:

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>

它也适用于空(默认)前缀,也可以为每个节点更改.以下是有效的 XML,每个节点都以自己的命名空间命名,甚至没有前缀:

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>

请注意,XPath 选择器中的前缀与任何给定 XML 文档中的前缀无关,并且必须具有前缀到名称空间的单独映射.同样在 XPath 中,前缀必须具有唯一的映射(它没有标准并且映射特定于每个 Xml DOM 实现).

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).

例如,对于本文中的第一个 XML,您不能使用类似于 XML 的 XPath,例如/a:r/a:p/a:c",因为每个前缀必须映射到不同的命名空间.Xpath 看起来像/p1:r/p2:p/p3:c",带有前缀映射 {p1->urn:one, p2->urn:two, p3->urn:three}.

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}.

这篇关于可以在 XML 文档中重用相同的命名空间吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆