Document directory
- I. [Introduction]
- Ii. [DTD (Document Type Definition )]
- Iii. XML Schema Constraints]
I. [Introduction]
XML is the Extensible Markup Language of extensible markup language. It is developed by SGML (Standard gerneralized Markup Language) and allows developers to customize tags and effectively separate tags from content. In XML technology, you can write a document to constrain the writing standards of XML documents, which is called XML constraints. This article mainly introduces two constraints: DTD and Schema (of course, there are other constraints, such as XDR and SOx, but they are rarely used)
Ii. [DTD (Document Type Definition )]
DTD is an early XML constraint. It also has many defects, especially in expressing the type constraints of attribute values and element content, the XML Schema mentioned later was born for customer service. (However, it is necessary to learn and understand the DTD. For example, many frameworks still use the DTD constraints)
1. Two DTD Constraints
DTD constraints can be written as a separate file or in an XML file.
(1) Write a dtd in an XML file
<? XML version = "1.0" encoding = "UTF-8" standalone = "yes"?> <! Doctype bookshelf [<! Element bookshelf (Book +)> <! Element book (title, author, price)> <! Element name (# pcdata)> <! Element author (# pcdata)> <! Price of element (# pcdata)>]> <bookshelves> <book title> JAVA employment training course </book title> <author> Zhang Xiaoxiang </author> <price> 39.00 RMB </price> </book> ... </bookshelves>
(2) Reference DTD Constraints
The XML file uses the doctype declaration statement to specify the DTD file it complies with. The doctype declaration statement has two forms:
A. When the Referenced File is local, use the following method:
<! Doctype document root node System "DTD file URL"> example: <! Doctype bookshelf System "book. DTD">
B. When the referenced file is a public file, use the following method:
<! Doctype document root node public "DTD name" "DTD file URL"> example: <! Doctype web-app public "-// Sun Microsystems, Inc. // DTD web application 2.3 //" http://java.sun.com/dtd/web-app_2_3.dtd ">
2. DTD constraint syntax details:
(1) DTD syntax details: Element Definition 1
Use element in the DTD document to declare an XML element. The syntax format is as follows:
<! Element element name element type> element type can be element content or type. For example, if it is an element content, you need to use () to enclose it, for example, <! Element bookshelf (title, author, price)> <! Element name (# pcdata)>
For example, if it is an element type, it is written directly. The DTD Specification defines the following types:
Empty: used to define empty elements, such as <br/> <HR/>
Any: indicates that the element content is of any type.
(2) DTD syntax details: Element Definition 2
You can use the following method to describe the composition of the content.
Separated by commas (,), indicating that the order in which the content appears must be consistent with that in the declaration. <! Element myfile (title, author, email)>
Separated by | indicates either of them, that is, only one
<! Element myfile (Title | author | email)>
You can also use +, *, and? In the element content ,*,? Equal sign indicates the number of times an element appears:
+: One or more (Books +)
? : 0 times or once (book ?)
*: 0 or multiple times (Book *)
You can also use parentheses () for batch setting. For example
<! Element myfile (Title *, author ?, Email) * | comment)>
(3) define attributes
The tag attribute in the XML document must be set through ATTLIST.
Syntax format:
<! ATTLIST element name attribute name 1 attribute value type setting description attribute name 2 attribute value type setting description...> Example of attribute Declaration: <! ATTLIST product category CDATA # required color CDATA # implied>
Corresponding XML file:
<Product category = "clothing" color = "yellow">... </Commodity> <Commodity Category = "apparel">... </Item>
Settings:
# Required: This attribute must be set.
# Implied: either set or not set
# Fixed: the value of this attribute is fixed to a value. Other values cannot be set for this attribute in XML files. However, you need to provide this value for this attribute.
Use the default value directly: You can set this value in XML or not set this attribute value. If not set, use the default value.
Example:
<! ATTLIST page author name CDATA # implied age CDATA # implied contact information CDATA # required website title CDATA # fixed "Page Author" personal interests CDATA "online">
(4) common attribute value types
CDATA: indicates that the attribute value is a common text string.
Enumerated
ID
Entity (entity)
A. Attribute Value Type-Enumerated
The attribute type can be a list of values. The attribute value set in the XML file can only be a value in the list (enumeration)
<? XML version = "1.0" encoding = "gb2312" standalone = "yes"?> <! Doctype shopping basket [<! Element meat empty> <! ATTLIST meat (Chicken | beef | pork | fish) "chicken">]> <shopping basket> <meat type = "fish"/> <meat type = "beef"/> <meat/> </shopping basket>
B. Property Value Type -- ID
Indicates that the property is set to a unique value.
The ID attribute value can only start with a letter or underscore and cannot contain blank characters.
<? XML version = "1.0" encoding = "gb2312"?> <! Doctype contact list [<! Element Contact List any> <! Element Contact (name, email)> <! Element name (# pcdata)> <! Element email (# pcdata)> <! ATTLIST contact ID # required>]> <contact list> <contact number = "1"> <Name> Zhang San </Name> <email> zhang@it315.org </Email> </contact> <contact number = "2"> <Name> Li Si </Name> <email> li@it315.org </Email> </contact list>
C. entity definition-entity
An object is used to create an alias for a section of content. You can reference this section in the XML document later.
In the DTD definition, one entry <! Entity…> A statement is used to define an object.
Entities can be divided into two types: Reference entities and parameter entities.
A. object definition-reference object
Referenced entities are mainly applied in XML documents.
Syntax format:
<! Entity Object Name "entity content">: directly convert to entity content
Reference Method:
& Entity name;
Example:
<! Entity copyright "I am a programmer">
......
& Copyright;
B. object definition-parameter entity
The parameter object is used by the DTD file.
Syntax format:
<! Entity % entity name "entity content">
Reference Method:
% Entity name;
Example 1:
<! Entity % tag_names "name | email | phone number | Address"> <! Element personal information (% tag_names; | birthday)> <! Element customer information (% tag_names; | company name)>
Example 2:
<!ENTITY % common.attributes " id ID #IMPLIED account CDATA #REQUIRED ">...<!ATTLIST purchaseOrder %common.attributes;><!ATTLIST item %common.attributes;>
Iii. XML Schema Constraints]
The schema is completely different from the DTD. The DTD uses another syntax different from the XML, and the schema itself is also an XML document. At the same time, the schema cannot meet the limitations in the DTD, this is especially true for the type constraints that express attribute values and element content (which is also a disadvantage of DTD ).
1. root tag
XML schema is a text file with the extension ". XSD". It is written in XML syntax. The heel mark of the XML Schema mode must be schema, And the namespace used is:
Http://www.w3.org/2001/XMLSchema
The prefix of the namespace. For example:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> ....</xsd:shema>
2. Element tag
The main purpose of the XML schema pattern is to constrain tags in XML files. In XML Schema mode, the "element" tag is used to constrain the tag in XML. You can use the "element" tag as the sub-tag of the root tag in XML Schema mode, and the "element" tag in XML Schema mode is abbreviated as an element. If an element is a sub-tag of the root tag, such an element is called a global element. The role of a Global element is to constrain the sub-tag at any level in the XML file, no matter which level of sub-mark the XML tag is in the XML file. For tags without sub-tags in XML files, the XML Schema mode is constrained by the "simple type" element. XML
The format of "simple type" elements in Schema mode is as follows:
<XSD: element name = "tagname" type = "simple data type">
The "tag name" is the tag name in the corresponding XML file, and the "simple data type" is a restriction on text data in the tag. For example, if the XML schema has the following elements:
<XSD: element name = "Date of Birth" type = "XSD: Date">
The text data in any tag named "birthdate" in the XML file that uses this pattern must be of the date type.
Simple data types that can be used in the XML Schema mode include int, float, double, date, time, and string.
For example:
<XSD: element name = "Date of Birth" type = "XSD: Date">
The "birthdate" in the element constraint XML file does not have a subtag, And the labeled data must be of the date type, that is, the content must be in the "yyyy-mm-dd" format.
3. Complex type Elements
For tags with sub-tags in XML files, the XML Schema mode can be constrained by the "complex type" element. The format of "complex type" elements in XML Schema mode is as follows:
<XSD: element name = "tag name"> <XSD: complextype> child tag constraint elements </XSD: complextype> </XSD: Element>
The most important part of a "complex type" element is the "Child tag constraint element". If the "complex type" element wants to constrain the tag sequence specified by name, several child tags will appear, in addition, these child tags are already bound by global elements in the pattern. Then, the "Child tag constraint elements" section can be elements with ref attributes, for example:
<XSD: element name = "tagname"> <XSD: complextype> <XSD: sequence> <XSD: Element ref = "sub-tag1"/> <XSD: element ref = "sub-tag 2"/> <XSD: sequence> </XSD: complextype> </XSD: Element> <XSD: element name = "sub-tag 1" type = "simple data type"/> If the constraints on the sub-tag do not want to reference a global element, or the Global element of the sub-tag is not bound, the "element bound to the Child tag" can also be a "complex type" element. For example, <XSD: element name = "tagname"> <XSD: complextype> <XSD: sequence> <XSD: element name = "tag1"> <XSD: complextype> ....................... </XSD: complextype> </XSD: elemnt> <XSD: sequence> </XSD: complextype> </XSD: Element>
4. Attributes
For attributes in XML files, the XML Schema mode uses "attribute" to mark constraints. The tag format is as follows:
<XSD: attribute name = "attribute name" type = "simple data type" use = "condition">
Optional values: "required", "optional", "fixed", and "default"
The "attribute" tag must be used in the "complex type" element to indicate the attributes of the XML tag bound by the "complex type" element, for example:
<XSD: element name = "tagname"> <XSD: complextype> <XSD: simplecontent> <XSD: Extension base = "XSD: string"> <XSD: attribute name = "student ID" type = "XSD: int" use = "required"/> </XSD: Extension> </XSD: simplecontent> </XSD: complextype> </XSD: Element>
5. Advantages of Schema
A. the schema itself uses XML syntax rules. users do not need to master new syntax rules.
B. schema provides powerful attribute representation and type support ....