XML (1) Namespace of--shema constraints

Source: Internet
Author: User
one, two constraints of XML DTD and schema
Excerpt from IBM's website: "XML DTD (XML document type definition) is the most widely used pattern in the field of XML technology in recent years." However, because XML DTD does not fully meet the requirements of XML automation processing, such as not well to implement the coordination between different modules of the application, lack of sufficient description of the constraints such as document structure, attribute, data type, etc., the XML schema was formally recommended by the consortium in May 2001 as XML Standard mode. Obviously, the consortium wants to use XML schemas as the mainstream of the XML Schema description language and gradually replace the XML DTD. Visible schema is used more and more, this article first elaborated the very important concept in the Shema constraint name Space

Second, Shema documents
Student.xsd
<?xml version= "1.0" encoding= "UTF-8"?>
<schema
xmlns= "Http://www.w3.org/2001/XMLSchema"
targetnamespace= "Http://www.example.org/student"
elementFormDefault= "Qualified" >
<element name= "Student" >
<complexType>
<sequence>
<element name= "num" type= "int"/>
<element name= "Name" type= "string"/>
<element name= "Birthday" type= "date"/>
</sequence>
</complexType>
</element>
</schema>
Student.xsd is a Shema file, which is also in XML format, and must conform to certain constraints. To understand the xmlns and targetnamespace through several questions.

question 1 How to guarantee the uniqueness of Shema files: targetnamespace
Because when referencing Shema files as XML constraints, imagine if there are more than one Shema file with the same name as the constraint. The by Shema The concept of namespaces to ensure uniqueness, the targetnamespace attribute is the namespace that specifies this XSD. Typically, a URL is used as a targetnamespace value to ensure uniqueness, and the URL usually does not necessarily exist.

question 2 How to introduce the Shema specification: xmlns
All of the tags and attributes of the XSD must conform to the schema specification, so where does the specification of element, ComplexType, sequence and so on come from.
The xmlns attribute is used to specify the Shema constraint. xmlns= "Http://www.w3.org/2001/XMLSchema" means that student.xsd labels and attributes that are not prefixed by default must conform to a schema constraint defined by w3s. If the Shema file is not defined by the consortium, you need to specify the location of the Shema file. The following XML introduces Student.xsd constraints.

What do you mean, question 3elementFormDefault?
This property is an enumeration value: Qualified, unqualified. By default, unqualified represents only the root tag student, and qualified represents all tags and attributes such as Num,name,birthday.


Third, XML file to introduce constraints
Method 1
Student.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<student xmlns= "Http://www.example.org/student"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://www.example.org/student student.xsd" >
<num>1000</num>
<name>xy</name>
<birthday>2000-01-01</birthday>
</student>
STUDENT.XML Specifies the xmlns= "http://www.example.org/student" constraint, which is a custom student.xsd. But as the problem ② says Shema files are not defined by the consortium, you need to specify the location of the Shema file.

question 4 How to specify an XSD location: schemalocation
Specify the Shema file location by schemalocation. but the schemalocation attribute is constrained by the http://www.w3.org/2001/XMLSchema-instance, so it is necessary to introduce this constraint again through xmlns. But a tag only allows an xmlns tag without a prefix, so give the new xmlns a prefix xsi, and the prefix name is customized. The following example is a good illustration of the use of prefixes:
<?xml version= "1.0" encoding= "UTF-8"?>
<xy:student
Xmlns:xy= "Http://www.example.org/student"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://www.example.org/student student.xsd" >
<xy:num>1000</xy:num>
<xy:name>xy</xy:name>
<xy:birthday>2000-01-01</xy:birthday>
</xy:student>

Method 2
Constraints are not determined by specifying the Shema namespace but by the XSD ask location.
<?xml version= "1.0" encoding= "UTF-8"?>
<xy:student
Xmlns:xy= "Http://www.example.org/student"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:nonamespaceschemalocation= "/studnet.xsd" >
<xy:num>1000</xy:num>
<xy:name>xy</xy:name>
<xy:birthday>2000-01-01</xy:birthday>
</xy:student>


The first paragraph is excerpted from: http://www.ibm.com/developerworks/cn/xml/x-sd/

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.