需要 XML 中的命名空间 [英] Need of namespace in XML

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

问题描述

我理解在 XML 中,如果两个不同的标签具有相同的 tagName,那么为了将它们分开,我们在它前面加上名称空间以使其唯一.但是当我们说

I understand that within a XML if I have same tagName for two different tags, then to separate the two of them, we precede it with the namespace to make it unique. but in the top when we say

<rootElement xmlns:myNameSpace="http://www.myNameSpace.com">

现在,为什么我们有这个 http://www.myNameSpace.com?除了它是独一无二的这一事实之外,这还有什么用途.

Now, why do we have this http://www.myNameSpace.com? What purpose does this serve, apart from the fact that it will be unique.

此外,我正在阅读有关 xslt 的信息,因为它也是一个 XML,因此它将命名空间定义为

Also, I was reading about xslt, and since it is also a XML then it defined the namespace as

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

现在,首先,这个链接告诉我什么,它有什么帮助在 xml 渲染中?那么它除了独特性之外还有其他用途吗?又为什么会走到这个地步?如果在 XML 中,我在两个标签之间存在冲突,我可以使用任意两个命名空间,比如 namespace1 和 namespace2 并使用它.

Now, first, what exactly does this link tell me and how does it help in xml rendering? So does it serve anything other than the uniqueness? And why go to such extent? If at all within a XML, I have conflict between two tags, I can just use any two namespaces, say namespace1 and namespace2 and work with it.

这里有我遗漏的东西吗?

Is there something I am missing here?

推荐答案

一般来说,XML 命名空间除了唯一标识与其关联的元素之外没有任何其他用途.

In general, an XML namespace does not serve any purpose apart from uniquely identifying the elements that are associated with it.

换句话说,字符串 "http://www.myNameSpace.com" 如下所示:

In other words, the String "http://www.myNameSpace.com" as in:

<rootElement xmlns:myNameSpace="http://www.myNameSpace.com">

纯粹是随意的.它不必指向任何地方.此外,所谓的前缀(在本例中为myNameSpace,紧接在xmlns:"之后的部分)是任意的,只是表示http://www.myNameSpace.com".

is purely arbitrary. It does not have to point anywhere. Also, the so-called prefix (in this case myNameSpace, the part right after "xmlns:") is arbitrary and just a shorthand way of saying "http://www.myNameSpace.com".

话虽如此,有几点保留:

Having said that, a few reservations:

1) 命名空间可以帮助构建大型文件中的 XML 数据,例如 OpenXML 格式的 Microsoft Word 文档:

1) Namespaces can help structure your XML data in large files, for example a Microsoft Word document in OpenXML format:

这是典型 OOXML 中存在的命名空间的摘录:

This is an excerpt of the namespaces present in typical OOXML:

xmlns:aml="http://schemas.microsoft.com/aml/2001/core"
xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

因此,虽然没有内在的理由使用单独的命名空间,但它有助于将您的 XML 词汇表划分为有意义的类别.

So, although there is no inherent reason to have separate namespaces, it helps dividing your XML vocabulary into meaningful categories.

2) 如您所见,对任意定义命名空间有一些限制:

2) There are a few constraints on arbitrarily defining namespaces, as you noticed:

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

这将元素标识为属于 XSLT 名称空间.而且,除此之外,这意味着这样标记的元素被 XSLT 处理器识别为不仅仅是 XML 代码,而是要执行的 XSLT 指令.

This identifies elements as belonging to the XSLT namespace. But also, beyond that, it means that elements thus marked are identified by an XSLT processor as not merely being XML code, but an XSLT instruction to be carried out.

链接http://www.w3.org/1999/XSL/Transform" 指向 XSLT 规范,其中规定了转换 XML 文档的规则.现在,回答您的问题:声明命名空间不会帮助转换.相反,如果您省略 XSLT 代码,XSLT 处理器将无法识别它.

The link "http://www.w3.org/1999/XSL/Transform" points to the XSLT specification, where the rules for transforming XML documents are laid down. Now, to answer your question: Declaring the namespace does not help the transformation. Rather, an XSLT processor does not recognize XSLT code if you omit it.

可以定义命名空间namespaceA"和namespaceB":

You can define the namespaces "namespaceA" and "namespaceB":

xmlns:nsA="namespaceA"
xmlns:nsB="namespaceB"

但是您不能使用它们来转换 XML,除非您只想更改前缀:

but you cannot use them to transform XML, unless you meant to just change the prefix:

xmlns:nsA="http://www.w3.org/1999/XSL/Transform"

这被认为是不好的做法.

which is considered bad practice.

这篇关于需要 XML 中的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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