No Nonsense xml--xml constraints (DTD)

Source: Internet
Author: User
Tags cdata file url xml parser

    • Basic terminology
First, preface prolog: Includes XML declaration (XML Declaration) and document type declaration (Doc type Declaration).
II. Good structure (well-formed specification): An XML document that conforms to the definition of a consortium.

    • Why does XML need to be validated?

Imposes additional constraints on the XML file for communication. For example: If I'm interacting with you with a piece of data, then according to my Supervisor consciousness I might define that, according to your subjective consciousness you might define that, so how do we interact with the data? Without a pre-defined convention to standardize how we write XML, then even this parsing estimate of XML is a problem, it also interacts with a yarn.

XML has 2 kinds of constraints: 1,DTD 2,schema.


    • DTD validation
Document type definition. The DTD defines the structure of the XML document content, ensuring that the XML stores the data in a consistent format.    Define the vocabulary accurately and constrain the content of the XML. A canonical XML document that conforms to a DTD is called a valid document.    The glossary, defined by the DTD, and the document syntax, the XML parser can check the validity of the XML document content. The canonical XML file is not necessarily valid, and the valid one must be normative.


1. DTD declaration
1) DTD declarations can be in a separate file
2) DTD declarations can be embedded in XML files
3) DTD declarations can be part of a separate file, and the other part is embedded in an XML file


2. Introduction of external DTD files. There are 2 types of doctype:

1), when the referenced file is local:

<! DOCTYPE root node name SYSTEM "DTD file path">

2), when the referenced file is a public file: In practice this situation is more common.

<! DOCTYPE root node name public "DTD name", "DTD file url" >

For example, we are writing a JSP, which is defined in the previous document.

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


3, DTD four kinds of tag declaration Elements (Element), attributes (Attlist), entities (entity), symbols (NOTATION)

1) Element XML element type declaration
Declaring elements: <! ELEMENT elementname (Contentmodel) >
The content of the element is described by the content pattern.
The types of DTD content patterns are:
The EMPTY element cannot contain any data, but it can have attributes (provided that its properties must be declared).
Cannot have child elements. Cannot have text data (including whitespace, line breaks).
DTD definition: <! ELEMENT elementname empty>
XML in:<elementname/> (recommended) or:<elementname></elementname>
(#PCDATA) Specifies that the element contains only the analytical character data, not the content type of any child elements of the type.
DTD definition: <! ELEMENT student (#PCDATA) >
Legal content in XML: <student>watching tv</student>
The (Elements) element is specified by the content mode part.
<! ELEMENT name (child particles) >
Content mode parts can be listed in the following table.
<! element name (b) > child elements A, b must appear, and in the order of the list
<! Element name (a|b) > select, child element A, B can only appear one
<! element name (a) > child element A can only and must occur once
<! element name (a) + > child element A appears one or more times
<! element name (a) * > child element A appears any number of times (including 0, one, and multiple)
<! ELEMENT name (a)? > child element A appears once or does not appear
Mixed Blending mode: Child elements can have both textual data and subordinate child elements.
<! ELEMENT RN (#PCDATA | an | en) *> "|" and "*" must be written.
The above sentence indicates that in Rn, the character data or en and an can appear any number of times, in order.
Priority (#PCDATA) such as: (#PCDATA |name) * Correct (name| #PCDATA) * ERROR
Any element can contain any type of data. Child elements (which must be defined in the DTD) and text data (including whitespace).
DTD definition: <! ELEMENT a any> <! ELEMENT b any>
Legal content in XML: <a>somngthing</a> or <a/> or <a><b>oo</b></a>

2) attribute (attlist) an allowable value declaration for a Property & property that can be set for a specific element type
<! Attlist elementname
AttributeName1 AttributeType Attributedefault
.......
Attributenamen AttributeType attributedefault>
Attribute type (Attribute type):
CDATA This property can only contain character data (note the difference from CDATA paragraphs, pcdata)
NmToken is a subset of CDATA, and its characters can only be letters, numbers, periods, dashes, underscores, or colons.
Nmtokens is similar to NMTOKEN, but this can contain multiple values, separated by a space between each value.
ID the value of this property is unique within the same document. An element can have only one attribute of type ID.
IDREF A similar pointer to the ID value declared elsewhere in the document. If the property value does not match the ID value pointed to, an error is returned.
IDREFS is similar to IDREF, but it can have multiple references separated by spaces.
Entity the value of this property must correspond to an entity that is declared inside the document but has not yet been parsed.
Entitys is similar to entity, but it can contain multiple entities separated by spaces.
NOTATION the value of this property must refer to the name of a comment that is declared elsewhere in the document.
(enumerated) A variable that is similar to an enumeration that must match the value listed. Use "|" For each value Separated.
such as: (Spring | summer | autumn | winter) The actual content document can only be taken from one of them.
Attribute attribute (Attribute Default):
#REQUIRED must have and only have one property.
#IMPLIED dispensable.
#FIXED define a default value in the DTD, the XML can be unspecified, and the specified must be equal to the default value.
Attribute-value If you do not specify a default value that is defined with a DTD, the specified value is used.


<! [cdata[############ attribute (attlist) example ##############]]>
Example One (#REQUIRED)
In the DTD: <! ELEMENT El (#PCDATA) > <! Attlist el at1 nmtokens #REQUIRED at2 CDATA #REQUIRED >
XML, correct: <el AT1 = "Ten" at2= "ten" >something</el>
XML, Error: <el at= "Ten" >something</el> (no property written for another #required AT2)


Example II (#IMPLIED, #FIXED)
In the DTD: <! ELEMENT El (#PCDATA) > <! Attlist El at CDATA #FIXED "Ten" AT2 CDATA #IMPLIED >
XML, correct: <el at2= ">something</el> (at has default value" ", AT2 can write not write)
XML, Error: <el at= "One" >something</el> (at either do not write, write can only be written as the default value)


Example three (Attribute-value)
In the DTD: <! ELEMENT El (#PCDATA) > <! Attlist El at CDATA "Ten" AT2 CDATA ">"
XML, correct: <el at= "one" >something</el>


Example four (enumerated + attribute-value)
In the DTD: <! ELEMENT El (#PCDATA) > <! Attlist el at (10|20|30) "Ten" >
XML, correct: <el at= ">something</el>" (at either do not write, default value 10; or (10|20|30) select a write)
<! [cdata[############ Property (Attlist) example is complete ##############]]>


3) Reusable content declarations for entities (entity)
Defining <! in DTDs Entity entity flag "solid content" >
Referencing custom entities in XML, using & entity flags; Represents the entity content.
4) symbol (NOTATION) do not parse the external content of the format declaration.

3. Internal entity: Write in XML file (use less). External entities: Also create a DTD file under the same XML folder (advocated)

External: The following is XML:
<?xml  version= "1.0"  encoding= "UTF-8" standalone= "no"?>      <! DOCTYPE root SYSTEM "GOODSINFO.DTD" ><!--Use this sentence to refer to external dtd-->      <root><goodsInfo>          < goodsname>goodsname</goodsname>          <goodsPrice>goodsPrice</goodsPrice>      </ Goodsinfo></root>
The following is a file named "Goodsinfo.dtd":
<! ELEMENT root   (goodsinfo) >      <! ELEMENT goodsinfo  (goodsname,goodsprice) >      <! ELEMENT goodsname  (#PCDATA) >      <! ELEMENT goodsprice (#PCDATA) >

In-house:
<?xml  version= "1.0"?>      <! DOCTYPE Root [          <! ELEMENT root (Student) >          <! ELEMENT student (#PCDATA) >          <! ENTITY CCTV  >      ]>  <!--to write DTD files inside the body--      <root><student>          Student Watch &CCTV;<!--using custom entities cctv-->      </student></root>

No Nonsense xml--xml constraints (DTD)

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.