XML series: (2) XML constraints

Source: Internet
Author: User
Tags file url

1. What is XML constraint


In XML technology, you can write a document that constrains the writing specification of an XML document, which is called an XML constraint.

2. The difference between XML syntax and XML constraints


The
The difference between XML syntax and XML constraints

functionperson who made
XML syntax Basic writing rules for canonical XML files Developed by the organization of the
XML constraints Rules for writing XML file data content format Self-defined by the developer


3. Common XML Constraint Technology


DTD constraints : The syntax is relatively simple and the function is relatively simple. Learning costs are also low.

schema Constraints : The syntax is relatively complex and functions are relatively powerful. The cost of learning is relatively high!!! (namespace)

4. XML constrained DTD


DTD (document Type Definition): Document type definition

4.1. Three ways to associate a DTD with an XML file


Three ways: Internal DTD, reference local DTD, and reference public DTD

4.1.1, Internal DTD


Grammar:

<! DOCTYPE root element [element declaration]>

Instance:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE note[<! ELEMENT Note (to,from,heading,body) > <! ELEMENT to (#PCDATA) > <! ELEMENT from (#PCDATA) > <! ELEMENT heading (#PCDATA) > <! ELEMENT Body (#PCDATA) >]><note> <to> USA </to> <from> China </from> 




4.1.2, referencing local DTD


Grammar:

<! DOCTYPE root element SYSTEM "file name" >

Instance:

File Note.dtd

<! ELEMENT Note (to,from,heading,body) ><! ELEMENT to (#PCDATA) ><! ELEMENT from (#PCDATA) ><! ELEMENT heading (#PCDATA) ><! ELEMENT Body (#PCDATA) >

File Note.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Note SYSTEM "NOTE.DTD" ><note> <to> USA </to> <from> China </from> 




4.1.3, referencing public DTDs


Grammar:

<! DOCTYPE root element Public "DTD name" "DTD file URL" >

Instance:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >



4.2. DTD syntax



4.2.1, elements


The element declaration uses the following syntax:

<! Element name Category >

Or

<! Element elements name (element content) >

Category:


The category of the element
type Type value function
Empty label EMPTY Indicates that the element must be an empty element
Normal string (#PCDATA) Indicates that the content of an element must be a normal string (cannot contain child tags)
Any Content Any Indicates that the content of an element can be anything (including sub-labels)

(element content)

Order problem: <! element name (child element name 1, child element name 2,.....) > sub-labels appear sequentially

Number of issues:

number of occurrences of the element
label meaning Grammar Example
Empty Must and only occur 1 times <! element name (child element name) > <! ELEMENT Note (message) >
+ appears at least 1 times <! element name (child element name +) > <! ELEMENT Note (message+) >
* 0 or N times <! element name (child element name *) > <! ELEMENT Note (message*) >
? 0 or 1 times <! element name (child element name?) > <! ELEMENT note (message?) >



4.2.2, properties


Grammar:

<! Attlist element Name Property Name property type default Value >

Or

<! Attlist element Name Property Name property Type default Value property Name property type default value ...>


Default value:

#REQUIRED property value is required

#IMPLIED properties are not required

#FIXED Value property is not required, but the property value is fixed

Property Type: Controls the value of the property.

CDATA: Represents a normal string

(en1|en2|.) : Indicates that one of the values must be selected, the enumeration value

ID: Indicates that the attribute value must be unique in an XML document. Value cannot start with a number



4.2.3, Entity


An entity is a variable that defines a shortcut that refers to plain text or special characters.

In a DTD definition, this entity can be referenced in an XML file by defining an entity.

Grammar:

<! Entity entity name "Value of Entities" >

Instance:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE persons[<! ENTITY Author "Zhang San" ><! ENTITY Home "Beijing, China" >]><Persons>&author; live in &home;</Persons>

Show:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/80/08/wKiom1c0ztuyMDN-AAAI5q3ceQA951.png "title=" Xml_ Entity.png "alt=" Wkiom1c0ztuymdn-aaai5q3ceqa951.png "/>

Note: An entity consists of three components: a number (&), an entity name, and a semicolon (;).

5. Schema


XML Schema is an XML-based DTD replacement.

The XML Schema describes the structure of the XML document.

The XML Schema language is also known as the XML Schema definition (XML Schema definition,xsd).


5.1. XML Schema and DTD


XML schemas conform to the XML syntax structure, which is itself an XML document

The structure of a DTD document is tiled, and if you define a complex XML document, it is difficult to grasp the nesting relationship between the elements, which is stronger relative to the document structure

XML schema supports more data types than DTDs and supports user-defined data types

XML schema is more powerful in defining constraints

XML Schema support for namespaces


5.2. Quick start of XML schema


The schema itself is an XML document, but the extension is. xsd.

A schema document is often called a constraint document, and an XML file that follows this constraint is called an instance document.

As with an XML file, a schema document must have a root node, and the name of the root node must be schema.

After writing a schema document, it is often necessary to bind the declared element in this file to a URI, and there is a technical term in XML Schema technology that describes the process of binding the elements declared in an XML Schema document to a namespace. Later, the XML file can tell the parsing engine through this URI, or namespace, where the elements written in the XML document come from and who are bound.

5.3. Example


Books.xsd (XML schema file)

<?xml version= "1.0"  encoding= "UTF-8"? ><schema xmlns= "Http://www.w3.org/2001/XMLSchema"  targetnamespace= "Http://www.rk.com/books"  elementformdefault= "qualified" ><!--  targetnamespace  represents the element definition declared in the current Xml schema document in the Http://www.rk.com/books namespace  --><!--  elementformdefault= "qualified" means that elements declared in this schema must be namespace-qualified  --><element name= "books" ><!-- The  name property represents the label name  --><complexType><!--  Complex element of the current element, defined by ComplexType  -->< sequence><!--  Sub-elements sequence  --><element name= "book"  maxoccurs= "unbounded" >< Complextype><sequence><element name= "title"  type= "string"/><!--  simple elements, Defined by the type attribute  --><element name= "Price"  type= "Double"/><any></any><!--   can write any  --></sequence><!--  property declaration   general write to  sequence  after  -->< Attribute name= "id"  type= "id"  use= "REquired "/></complextype></element></sequence></complextype></element></ Schema>


Books.xml (XML instance file)

<?xml version= "1.0" encoding= "UTF-8"? ><rk:books xmlns:rk= "http://www.rk.com/books" xmlns:xsi= "http// Www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" Http://www.rk.com/books books.xsd "><!--xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "is a fixed notation, the instance namespace--><!--Xsi:schemalocation This property has two values: the first value is the namespace you need to use The second value is the location of the XML schema used by the namespace-<rk:book id= "B1" > <rk:title> the past is not like cigarettes </rk:title> <rk:price> 49.9</rk:price> </rk:book> <rk:book id= "B02" > <rk:title>xml learning </rk:title> <rk:price >23.2</rk:price> </rk:book></rk:books>

Bookswithdefaultnamespace.xml (XML instance file)

<?xml version= "1.0" encoding= "UTF-8"? ><books xmlns= "Http://www.rk.com/books" xmlns:xsi= "http://www.w3.org /2001/xmlschema-instance "xsi:schemalocation=" Http://www.rk.com/books books.xsd "><!--using the default namespace format: xmlns=" Http://www.rk.com/books "--><!--an XML instance document allows only one default namespace--<book id=" B1 "> <title> last Aristocracy </ Title> <price>49.9</price> </book></books>





XML series: (2) XML constraints

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.