Differences between constraints and namespaces in XML

Source: Internet
Author: User

XML constraints when learning XML, because XML writing is very flexible, so we generally write a document to constrain the writing specification of the XML document, and this constraint XML document document becomes the XML constraint, There are two kinds of constraints, DTD and Scheam, respectively, the former is relatively old, and can not fully meet the requirements of XML automatic processing (this sentence is not very understanding), the latter is the 2001 is recommended developers to use, can be a good document properties, data types, structure and other aspects of the description , The following examples illustrate:

This is a very simple XML that describes a book, which uses DTD constraints

Book.xml

<?xml version="1.0" encoding="UTF-8"?>

<! DOCTYPE Bookshelf SYSTEM "BOOK.DTD" >

< bookshelf >

< books >

< title >java programming thought </title >

< author > Zhang San </author >

< price >89.00 Yuan </price >

</book >

< books >

< title >javascript Web Development </title >

< author > John Doe </author >

< price >28.00 Yuan </price >

</book >

</Bookshelf >

Its constraints book.dtd is so written, we should look at a glance, better understand

<! ELEMENT Bookshelf (book +) >

<! ELEMENT Book (title, author, price) >

<! ELEMENT title (#PCDATA) >

<! ELEMENT author (#PCDATA) >

<! ELEMENT Price (#PCDATA) >

In contrast, let's look at how the constraints of a schema are written.

Book. xsd
<?xml version= "1.0" encoding= "UTF-8"?>
<schema
xmlns = "Http://www.w3.org/2001/XMLSchema" &NBSP;
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= "Prices" type= "int" />
</complextype
</ELEMENT></SCHEMA>

< Span style= "WHITE-SPACE:PRE;" >schema constraint itself is also an XML, and it has its own constraints, and we don't care about that, Let's take a look at some of its important properties in the following questions

< Span style= "WHITE-SPACE:PRE;" > question 1 How to guarantee Shema file uniqueness: TargetNamespace (aka namespace)
Because when referencing Shema files as XML constraints, imagine if more than one Shema file has the same name, which one of the constraints will prevail? to ensure uniqueness by Shema the concept of namespaces, targetnamespace

property is the namespace that specifies this XSD. usually the URL is used as the value of the targetnamespace to ensure uniqueness, and the URL is usually not necessarily present.

< Span style= "WHITE-SPACE:PRE;" > question 2 How to introduce the Shema specification: xmlns
Span style= "color: #000000;" >xsd all tags and attributes must also conform to the schema specification, where does the specification for the element, ComplexType, sequence, etc. come from? specifies the Shema constraint through the xmlns attribute.

< Span style= "WHITE-SPACE:PRE;" >xmlns= "Http://www.w3.org/2001/XMLSchema" This means that student.xsd default tags and attributes that are not prefixed must conform to a schema constraint defined by w3s.

If the Shema file is not defined by the organization, you need to specify the location of the Shema file. The following XML introduces Student.xsd constraints when introduced.

What does problem 3elementFormDefault mean?
This property is an enumeration value: Qualified, unqualified. By default, unqualified indicates that only the root tag student is associated, and qualified means that all tags and attributes, such as Num,name,birthday, are associated.

question 4 How to specify the XSD location: schemalocation
Specifies the Shema file location through schemalocation.

But one tag only allows a non-prefixed xmlns tag, so give the new xmlns a prefix xsi, The prefix name is customized. The following examples illustrate 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>

Differences between constraints and namespaces in XML

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.