What is xmlns--xml Namespace explained

Source: Internet
Author: User
Tags xml parser xsl

Keep looking at struts recently and see this when looking at Struts.xml configuration:

xmlns= "Http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation = "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

Think of myself in the previous blog, someone asked this question, and then I am also unfamiliar with XML, so a systematic study, the basic attributes, XML dom (in fact, and HTML DOM similar), Dtd,xml Schema,xmlns.


Similar to this technical knowledge, direct Google is good, this will save a lot of time.

First seen from StackOverflow: Http://stackoverflow.com/questions/1181888/what-does-xmlns-in-xml-mean


Q: I see this sentence in the XML file, xmlns, what is this?

Xmlns:android= "Http://schemas.android.com/apk/res/android"

A: It defines an XML namespace.

In your example, the namespace prefix is Android, and the namespace URI is

Http://schemas.android.com/apk/res/android
in the document, you can see elements such as <android:foo>.

In this way, the namespace prefix is a variable that represents the entire namespace URI in a short name.

and This is equivalent to writing. This is read by the XML parser when it is read.


Reference: http://www.sitepoint.com/xml-namespaces-explained/#


Refer to the article is better, in this part of the book is too simple, so it is still translated this article.

XML Namespaces explainedWhy do we need an XML namespace?

Here are two basic requirements for the existence of a namespace:

1. Eliminate the ambiguity of two elements that have exactly the same name.

2. Group the elements of Common idea together.


These descriptions are a bit vague and look directly at examples:

Eliminate the ambiguity of two elements that have exactly the same name


Consider:

* in the (x) HTML file There is a TABLE element, and XSL-FO (now called XSL, the role can be analogous to the role of CSS to HTML) also has the same name element.

* Both A,title and style are (x) elements of HTML and SVG (SVG is the language that uses XML to describe two-dimensional graphics and drawing programs).

Well, how do you tell the difference between the title of SVG and the title of HTML?


Grouping elements of common idea together

In the (X) HTML file, Table,style and a elements are managed by the special rules required, some may contain, some may not.

Define the required rules should be included in the same place.


For example, my XML-based data should have validation rules, and I want to:

* Define these rules in the same place.

* Differentiate these special rules from other sets of rules that I (or others) define.


What is a namespace?

The namespace is a unique URI.


The advantage of this format is that any person who transmits XML can pretend to be getting the domain name (the bit after the http:/, but before the next/This sentence does not know how to translate, hope someone points out, thank you), But relying on someone else's domain name is not good (especially if they don't know you do it).


In the XML document, the URI is associated with the prefix, and each element uses a prefix that indicates which namespace the element belongs to. For example:

Rdf:description  Xsl:template  

In these examples:


* The front part of the colon is prefixed

* The part after the colon is the local part

* Any element with a prefix is a canonical name

* Elements without a prefix are names that do not conform to the specification


How do I use namespaces?

Using namespaces, first contact the namespace and the URI.

<foo:tag xmlns:foo= "Http://me.com/namespaces/foofoo";.

Foo is defined as the prefix of the element tag namespace. xmlns is concatenated as a prefix and this attribute, as in the command "Connect these letters and URIs". A non-canonical document has two identical attributes, and a colon can prevent the same prefix from being defined two times at a time.


Define a namespace prefix

Here's an example that defines a namespace prefix:

<foo:tag xmlns:foo= "Http://me.com/namespaces/foofoo" >    <foo:head>     <foo:title>an Example document</foo:title>   </foo:head>    <foo:body>     <foo:e1>a Simple document</ Foo:e1>     <foo:e2>       Another element     </foo:e2>   </foo:body> </foo:tag>

All elements are <foo:tag> inside, and the namespace prefix foo is associated with it http://me.com/namespaces/foofoo .


Define multiple prefixes in the same namespace

Different prefixes represent different namespaces that are possible, as follows:

<tag>      <foo:head xmlns:foo= "Http://me.com/namespaces/foofoo" >      <foo:title>an Example document</foo:title>    </foo:head>      <bar:body xmlns:bar= "Http://me.com/namespaces/foofoo" >      <bar:e1>a simple document</bar:e1>      <bar:e2>        another element      </bar:e2 >    </bar:body>  <tag>

Different namespaces define the same prefix

It is also possible to define the same prefixes for different namespaces (but not recommended), depending on the context:

<myns:html xmlns:myns= "http://www.w3c.org/1999/xhtml" >    <myns:head>  <myns:title>a Really bad idea</myns:title>  </myns:head>    <myns:body>    <myns:h1>a really bad idea</myns:h1>      <myns:pre>        <myns:pre xmlns:myns= "Http://my.com/namespaces/test-data" >          <myns:table>            <myns:data>              Hello World            </myns:data>          </myns:table>        </myns:pre>     </myns:pre>  </myns:body>

Note: This is not a good idea!


Multiple namespaces

If you use more than one namespace, you will undoubtedly need to use multiple namespaces at the same time-so how do you declare multiple namespaces at the same moment.


All you need to do is use multiple xmlns declarations, just like this:

<foo:tag xmlns:foo= "Http://me.com/namespaces/foofoo"           xmlns:bar= "Http://me.com/namespaces/foobar           " >      <foo:head>      <foo:title>an example document</foo:title>    </foo:head>      <bar:body>      <bar:e1>a Simple document</bar:e1>      <bar:e2>        another element      </bar:e2>    </bar:body>  </foo:tag>

The default namespace

Q: When using any namespace, all elements need to exist within a namespace.

A: Yes, but it won't be a problem.


Defining a namespace but it is not allowed to associate with prefixes-they are not canonical names in the example above.


This is the importance of XHTML, and as a requirement of this language, XHTML does not replace html--html or prefixes.


Define the default namespace without a prefix and directly assign xmlns to it:

<span style= "FONT-SIZE:18PX;" ><xhtml xmlns= "http://www.w3c.org/1999/xhtml" > </span>

Example:




Properties and namespaces and what I should put in the namespace URI at the end of the translation.



Back to the question:

xmlns= "Http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation = "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"


Combined with the tutorial:

XML Schema is an XML-based DTD replacement.
The XML Schema describes the structure of the XML document.
The XML Schema language is also known as the XML Schema definition (XML Schema definition,xsd).

In the DTD, the role of a DTD (document type definition) is to define the legitimate building blocks of XML documents.

In the final analysis, namespaces appear to distinguish between elements of the same name that have different meanings.

You can distinguish between your own and my elements through a namespace:

Xmlns:you= Http://you and xmlns:me=http://me are two different namespace prefixes.

If it is Xmlns=uri, it represents the default namespace for the document.


Now, the story is still right:

Xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"
tells the schema authenticator that all the elements in the XML namespace are HTTP://JAVA.SUN.COM/XML/NS/J2EE


Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
the namespace that xsi is prefixed to. In combination with the above example, Xsi:schemalocation is http://www.w3.org/2001/XMLSchema-instance/xsi:schemaLocation.


xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

Discover no, there are two values, the first is the value of the namespace, the second value is the location of the XML schema used by the namespace, and is known by the above: The XML Schema describes the structure of the XML document.


Problem solving.


What is xmlns--xml Namespace explained

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.