The role of the namespace. The following content is reprinted. You can see the problem: the element name of XML is not fixed, when two different documents use the same name to describe two different types of elements, or use the same tag to indicate two different meanings, a name conflict occurs. Solution to the Problem: namespace provides a unique identifier for the DTD of each specific application. If the elements defined in the DTD are used in XML, the identifier, element name, and attribute of the DTD must be used together, indicating where the element comes from, so that it will not be confused with other elements with the same name (a bit similar to the role of the Java package, ). In XML, the existing domain name is used as the namespace, that is, the URL is used as the namespace of XML. The namespace allows us to combine different element and attribute definitions in a document and specify that the definitions of these elements and attributes come from there. The namespace syntax is as follows: xmlns: [prefix] = "[url of name]" where "xmlns:" is a required attribute. "Prefix" is the alias of 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 worth noting that the batch-list, batch, and other markup must be defined in the http://www.lsmx.net.ac and alias is INS. Note that the namespace must be declared before use. Do not forget ":" during use. 1. The default namespace xmlns = "[url of namespace]" 2. specifies the namespace of the parent element. The child element wants to use its own namespace, you can specify the namespace alias in the sub-element. 3. attributes can also have their own namespaces. Xsi: schemalocation
Reference Mode document in an instance
XML Schema provides two special attributes used in the instance document to indicate the location of the mode document. These two attributes are: xsi: schemalocation and xsi: nonamespaceschemalocation. The former is used to declare the mode document of the target namespace, and the latter is used for the mode document without the target namespace, they are usually used in the instance documentation.
4.5.7.1 xsi: schemalocation attributes
Xsi: the value of the schemalocation attribute is composed of a URI reference pair. The two Uris are separated by spaces. The first URI is the name of the namespace. The second URI represents the location of the mode document from which the mode processor reads the mode document, the target namespace of this mode document must match the first Uri. See 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 explanation </title> <author> sun Xin </author> </book> |
Declare the default namespace (http://www.sunxin.org/book ).
② Declare the XML schema instance namespace (http://www.w3.org/2001/XMLSchema-instance) and bind the xsi prefix to the namespace so that the pattern processor can recognize the xsi: schemalocation attribute. The prefix of an XML schema instance namespace usually uses xsi.
③ Use the xsi: schemalocation attribute to specify the namespace http://www.sunxin.org/bookand the mode location http://www.sunxin.org/book.xsd. In this example, the target namespace declared in book. XSD must be http://www.sunxin.org/book.
A possible schema document book. XSD is shown in Figure 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 attribute can also be composed of multiple URI reference pairs. Each URI reference pair is separated by a blank character. Example 4-30 uses multiple namespaces. The value of the xsi: schemalocation attribute 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 deep programming </title> <author> <P: Name> James </P: Name> <P: title> writer </P: title> </author> </book> <title> XML entry to proficiency </title> <author> <P: name> Li Si </P: Name> <P: title> instructor </P: title> </author> </book> </books> |
The XML schema recommendation standard states that the xsi: schemalocation attribute can be used on any element of the instance, not necessarily the root element. However, xsi: the schemalocation attribute must appear before any elements and attributes to be verified.
In addition, the XML schema recommendation standard does not require that the pattern processor use the xsi: schemalocation attribute. Some pattern processors can obtain the location of the pattern document in other ways, ignore the xsi: schemalocation attribute.
Xsi: nonamespaceschemalocation attribute
Xsi: The nonamespaceschemalocation attribute is used to reference a pattern document without a target namespace. Unlike the xsi: schemalocation attribute, the value of the xsi: nonamespaceschemalocation attribute is a single value, which is only used to specify the location of the mode document. Example 4-31 shows how to use the xsi: nonamespaceschema location attribute in the instance documentation.
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 explanation </title> <author> sun Xin </author> </book> |
Like the xsi: schemalocation attribute, the xsi: nonamespaceschemalocation attribute can also be used on any element in the instance, not necessarily the root element. However, xsi: the nonamespaceschemalocation attribute must appear before any elements and attributes to be verified.
In addition, the XML schema recommendation standard does not require that the pattern processor use the xsi: nonamespaceschemalocation attribute. Some pattern processors can obtain the location of the pattern document in other ways, ignore the xsi: nonamespaceschemalocation attribute.