The role of XML namespaces (xmlns)

Source: Internet
Author: User

http://blog.csdn.net/zhch152/article/details/8191377*********************************************************The role of the namespace, the following content is reproduced, you can look at: the problem: XML element name is not fixed, when two different documents use the same name to describe two different types of elements, or a same token represents two different meanings of content, a naming conflict occurs. Problem Solving: namespace (Namespace), for each set of specific application DTD, give it a unique flag to represent, if the elements defined in the DTD are used in the XML, the DTD flags and element names, attributes should be used together, the equivalent of indicating where the elements come from, This will not be confused with other elements of the same name (a bit like the role of a package in Java, give it a full name). In XML, a ready-made, globally unique "domain name" is used as the namespace, which is the URL as the namespace of the XML. Namespaces allow us to combine different elements and attribute definitions in a document, and to indicate where the definitions of those elements and attributes come from. The syntax for the namespace is as follows: xmlns:[prefix]= "[URL of name]" where "xmlns:" is a required attribute. "prefix" is an alias for the namespace, and its value cannot be XML. <sample xmlns:ins= "Http://www.lsmx.net.ac" > <ins:batch-list> <ins:batch>evening batch</ins: Batch></ins:batch-list> </sample> It is important to note that tokens such as batch-list,batch must be defined in "Http://www.lsmx.net.ac", and the alias is ins. Note that you must declare the namespace before you use it, and don't forget ":" When you use it. One, the default namespace xmlns= "[url of Namespace]" two, specifies the namespace of the parent element, the child element wants to use its own namespace, you can specify the namespace alias in the child element. Third, attributes can also have their own namespaces.

Xsi:schemalocation detailed

Referencing schema documents in an instance

The XML schema provides two special attributes that are used in the instance document to indicate the location of the schema document. The two properties are: Xsi:schemalocation and xsi:nonamespaceschemalocation, which are used to declare schema documents for the target namespace, which are used in schema documents that do not have a target namespace, and are typically used in instance documents.

4.5.7.1 Xsi:schemalocation Properties

The value of the Xsi:schemalocation property consists of a URI reference pair, separated by a space character between two URIs. The first URI is the name of the namespace, the second URI gives the location of the schema document, and the pattern processor reads the schema document from this location, and the target namespace of the schema document must match the first URI. Let's look at example 4-28.

Example 4-28 book6.xml

<?xml version= "1.0" encoding= "GB2312"? ><book xmlns= "Http://www.sunxin.org/book" ①xmlns:xsi= "http// Www.w3.org/2001/XMLSchema-instance "②xsi:schemalocation=" Http://www.sunxin.org/book http://www.sunxin.org/ Book.xsd ">③<title> Struts 2 in Depth" </title><author> Xinxin Sun </author></book>

① declares the default namespace (Http://www.sunxin.org/book).

② declares the XML Schema instance namespace (http://www.w3.org/2001/XMLSchema-instance) and binds the XSI prefix to the namespace so that the schema processor can recognize the Xsi:schemalocation attribute. The prefix of the XML schema instance namespace is typically used with XSI.

③ uses the Xsi:schemalocation property to specify the namespace Http://www.sunxin.org/book and the mode location http://www.sunxin.org/book.xsd related. Note that in this example, the target namespace requirement declared in Book.xsd is Http://www.sunxin.org/book.

A possible schema document is book.xsd as shown in example 4-29.

Example 4-29 book.xsd

<?xml version= "1.0" encoding= "UTF-8"? ><xs:schema xmlns:xs= "http://www.w3.org/2001/XMLSchema" xmlns= "Http://www.sunxin.org/book" targetnamespace= "Http://www.sunxin.org/book" elementformdefault= "qualified" ><xs:element name= "book" type= "BookType"/><xs:complextype name= "BookType" ><xs:sequence>< Xs:element name= "title" Type= "xs:string"/><xs:element name= "author" type= "Xs:string"/></xs:sequence > </xs:complexType></xs:schema>

In fact, the value of the Xsi:schemalocation property can also consist of multiple URI reference pairs, separated by a white space character between each URI reference pair. Example 4-30 instance document uses more than one namespace, and the value of the Xsi:schemalocation property contains two pairs of URIs.

Example 4-30 books.xml

<?xml version= "1.0" encoding= "GB2312"? ><books xmlns= "Http://www.sunxin.org/bks" xmlns:p= "http// Www.sunxin.org/people "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation="/HTTP/ Www.sunxin.org/bks bks.xsdhttp://www.sunxin.org/people people.xsd "><book><title>jsp in-depth programming </title ><author><p:name> Zhang San </p:name><p:title> writers </p:title></author></book> <book><title>xml from getting started to mastering </title><author><p:name> John Doe </p:name><p:title> Teachers </p:title></author></book></books>

The XML Schema recommendation standard indicates that the Xsi:schemalocation property can be used on any element in the instance, not necessarily the root element, but theXsi:schemalocation property must appear before any elements and attributes it wants to validate.

Also, it is important to note that the XML schema recommendation does not require that the schema processor have to use the Xsi:schemalocation property, and some schema handlers can get the location of the schema document in other ways, ignoring the Xsi:schemalocation property.

Xsi:nonamespaceschemalocation Property

The Xsi:nonamespaceschemalocation property is used to reference a schema document that does not have a target namespace. Unlike the Xsi:schemalocation property, the value of the Xsi:nonamespaceschemalocation property is a single value and is used only to specify the location of the schema document. Example 4-31 shows the use of the Xsi:nonamespaceschema location property in the instance document.

Example 4-31 book7.xml

<?xml version= "1.0" encoding= "GB2312"? ><book xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: nonamespaceschemalocation= "book.xsd" isbn= "978-7-121-06812-6" ><title> "Struts 2 in Depth" </title>< Author> Xinxin Sun </author></book>

As with the Xsi:schemalocation property, the Xsi:nonamespaceschemalocation property can also be used on any element in the instance, not necessarily the root element, but xsi: The noNamespaceSchemaLocation property must appear before any elements and attributes it wants to validate.

Also, it is important to note that the XML schema recommendation does not require that the schema processor have to use the Xsi:nonamespaceschemalocation property, and some schema handlers can get the location of the schema document in other ways, ignoring the xsi: The noNamespaceSchemaLocation property.

The role of XML namespaces (xmlns)

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.