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

查看:20
本文介绍了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"

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

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

纯粹是任意的.它不必指向任何地方.此外,所谓的 prefix(在本例中为 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天全站免登陆