Experience 3--xml constraint--schema

Source: Internet
Author: User

The 1.XML schema is also a schema language for defining and describing the structure and content of XML documents, which appears to overcome the limitations of DTDs

L XML Schema VS DTD:

The XML schema conforms to the XML syntax structure.

XML APIs such as Dom and sax can easily parse the contents of a XmlSchema document.

The XML Schema supports namespaces very well.

The XML Schema supports more data types than XML DTDs, and enables users to customize new data types.

The XML Schema defines constraints with a strong ability to make detailed semantic restrictions on XML instance documents.

The XML Schema cannot define entities like DTDs, which is more complex than DTDs, but XML schemas are now the standard of the organization of the consortium, which is gradually replacing DTDs.

2.Schema Constraints QuickStart

The XML Schema file itself is an XML file, but its extension is usually. xsd.

L An XML Schema document is often called a schema document (a constraint document), and an XML file that follows this document is called an instance document .

L and XML files, an XML Schema document must also have a root node, but the name of the root node is schema.

• After writing an XML Schema constraint document, it is often necessary to bind the elements declared in this file to a URI address, and there is a technical term in the XmlSchema technology that describes the process, which binds the elements declared by an XML Schema document to a namespace , the XML file can then tell the parsing engine through this URI (namespace), where the elements written in the XML document come from and who is bound.

Example:

. xsd file

<?xmlversion= "1.0" encoding= "UTF-8"?>

<xs:schemaxmlns:xs= "Http://www.w3.org/2001/XMLSchema"

Targetnamespace= "http://www. Itcast.cn "

elementformdefault= "qualified" >

<xs:element name= ' Bookshelf ' >

<xs:complexType>

<xs:sequence maxoccurs= ' unbounded ' >

<xs:element name= ' book ' >

<xs:complexType>

<xs:sequence>

<xs:element name= ' title ' type= ' xs:string '/>

<xs:element name= ' author ' type= ' xs:string '/>

<xs:element name= ' selling price ' type= ' xs:string '/>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:schema>

. xml file

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

<itcast: Bookshelf xmlns:itcast= "http://www.itcast.cn"

Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

xsi:schemalocation= "Http://www.itcast.cnbook.xsd" >

<itcast: Books >

<itcast: Title >javascript Web page Development </itcast: title >

<itcast: Author >redarmy</itcast: author >

<itcast: Price >28.00 yuan </itcast: Price >

</itcast: Books >

</itcast: Shelves >

3. Concept of namespaces

In an XML schema, each constraint schema document can be assigned a unique namespace, represented by a unique URI (uniform Resource Identifier, Uniform Resource Identifier). When you write a label in an XML file, you can declare by namespace declaration (xmlns) that the currently written label comes from which schema constraint document. Such as:

<CSDN: Bookshelf xmlns:csdn= "Http://www.csdn.net" >

<itcast: Book >......</itcast: Book >

</CSDN: Shelves >

Here you use CSDN to point to the name of the declaration so that you can refer to the namespace later.

Note: The name syntax for namespaces is confusing, although starting with http://, that URL does not point to a file that contains schema definitions. In fact, this url:http://www.csdn.net does not point at all to any file, just an assigned name.

In order to declare the exact location of the schema file it follows in an XML document, it is usually necessary to specify the root node in the XML document using the schemalocation attribute, for example:

<itcast: Bookshelf xmlns:itcast= "http://www.itcast.cn"

Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

xsi:schemalocation= "Http://www.itcast.cnbook.xsd" >

SchemaLocation This property has two values. The first value is the namespace that needs to be used. The second value is the location of the XML schema used by the namespace, separated by a space. Note: When you use the SchemaLocation property, you also need to specify where the property comes from .

4. Use the default namespace

L Basic Format:

Xmlns= "URI"

L Examples:

< bookshelf xmlns="Http://www.it315.org/xmlbook/schema"

Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

xsi:schemalocation= "Http://www.itcast.cnbook.xsd" >

< book >

< title >javascript Web Development </title >

< author >redarmy</>

< price >28.00 Yuan </price >

</book >

< shelves >

introducing multiple XML Schema documents using namespaces

List of documents: Xmlbook.xml

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

< bookshelf xmlns= "Http://www.it315.org/xmlbook/schema"

Xmlns:demo= "Http://www.it315.org/demo/schema"

xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

xsi:schemalocation= "Http://www.it315.org/xmlbook/schema http://www.it315.org/xmlbook.xsd

Http://www.it315.org/demo/schemahttp://www.it315.org/demo.xsd ">

< book >

< title >javascript Web Development </title >

< author >redarmy</>

< price Demo: currency = "RMB" >28.00 yuan </selling price >

</book >

</Bookshelf >

L do not use namespaces to introduce XML Schema documents

List of documents: Xmlbook.xml

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

< bookshelf xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

xsi:nonamespaceschemalocation= "Xmlbook.xsd" >

< book >

< title >javascript Web Development </title >

< author >redarmy</>

< price >28.00 Yuan </price >

</book >

</Bookshelf >

L declare namespaces in XML Schema documents

<xs:schemaxmlns:xs= "Http://www.w3.org/2001/XMLSchema"

Targetnamespace= "http://www.itcast.cn"

elementformdefault= "qualified" >

<xs:schema>

The L targetnamespace element is used to specify which namespace the element declared in the schema document belongs to.

The L elementFormDefault element is used to specify that the root element and all of its child elements declared in the schema document belong to the namespace specified by targetnamespace.

For example, the elements in this example do not belong to any namespace, but we want to show that these terms are from the Xmlbook.xsd schema document and can be specified by the noNamespaceSchemaLocation attribute.

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.