XML中的名称空间需求 [英] Need of namespace in XML

查看:111
本文介绍了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:"之后的部分)是任意的,只是表示"

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天全站免登陆