Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

xslt - Need of namespace in XML

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

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

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"

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

<rootElement xmlns:myNameSpace="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) Namespaces can help structure your XML data in large files, for example a Microsoft Word document in OpenXML format:

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"

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

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

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

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.

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.

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...