Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of XML File constraint DTD

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you the example analysis of XML file constraints DTD, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

A brief introduction to 1.XML File constraints and DTD

We write documents to constrain the writing specification of an XML document, which is called a XML constraint.

Commonly used constraint techniques are:

XML DTD

XML Schema

The basic concepts of DTD:

Document type definition document type definition

DTD files are generally used in conjunction with XML files, mainly to constrain XML files.

The XML file introduces the DTD file so that XML can customize the tags but is constrained by the DTD file. For example, the previous section uses XML to describe the information of a class. If we define a tag for each student, there is no grammatical error, but it is not semantic. How can students describe it in terms of area? At this point, we need to use the DTD file to constrain the XML.

Yang Guo male 20 100 1.1 DTD constraint Quick start case

Basic syntax:

We also take the class as an example and write the following DTD file, myClass.dtd:

The first line indicates that the root element is a class and has a student as a child element, with one or more child elements.

The second line indicates that the student's child elements are name, age, and introduction.

There are no child elements under the name, so # PCDATA means that you can put any text in the name.

Age and introduction are also similar.

Write the myClass.xml file and introduce the DTD file as follows:

Zhou Xiaoxing 23 studies hard Lin Xiao 25 is a good student

Written in introduction: SYSTEM, indicating that the current DTD file is local

If you write PUBLIC, it means that the introduced DTD file is from the network.

The DTD file introduced at this time has no effect. If we add child elements to the student element and open the XML file, the browser will still not report an error.

Zhou Xiaoxing 23 studies hard 100 square meters Lin Xiao 25 is a good student

We need to program to verify the correctness of the XML document.

Browsers above IE5 have built-in XML parsing tool: Microsoft.XMLDOM, developers can write JavaScript code, use this parsing tool to load XML files, and DTD verification of XML files.

We write myXmlTools.html to verify this XML, as follows:

/ / create xml document parser object var xmldoc = new ActiveXObject ("Microsoft.XMLDOM"); / / enable xml verification xmldoc.validateOnParse = "true"; / / load xml document, that is, specify which XML file to validate xmldoc.load ("myClass.xml"); [xss_clean] ln ("error message:" + xmldoc.parseError.reason+ ")

"); [xss_clean] ln (" error line number: "+ xmldoc.parseError.line)

Open the html file with an IE browser, and you can see the running result:

You can see that line 9 is the same line we added.

2.DTD details 2.1 statements and references to DTD documents

1. Internal DTD document

two。 External DTD document

There are two types of imported DTD documents:

(1) when the referenced DTD file is a local file, identify it with SYSTEM and write "the file path of DTD", as follows:

(2) if the referenced DTD file is a public file, use the PUBLIC logo as follows:

Such as the following example:

2.2 basic DTD syntax:

Where:

-ELEMENT is a keyword and cannot be modified.

-NAME represents the element name

-CONTENT is an element type and must be capitalized! The content of CONTENT can be written in three ways:

(1) EMPTY-- means that the element cannot contain child elements and text, but can have attributes.

(2) ANY-- means that the element can contain any element content defined in the DTD

(3) # PCDATA-- can contain any character data, but it cannot contain any child elements

2.3 combination type of DTD elements:

The DTD states as follows:

This DTD specifies that the family element can have one or more "people" as child elements, or 0 to more "home appliances" as child elements. The meaning of the plus sign "+" and the asterisk "*" is the same as that in the regular expression.

XML wrote:

With regard to combination types, the following modifiers are available:

The symbolic usage example shows that () is used to group elements (Gulong | Jin Yong) and (Wang Shuo | Yu Jie) into two groups | selecting one of the listed objects (man | woman) means that a man or woman must appear. At least one of them + the object must appear once or more times (member +) indicates that the member must appear. However, there can be multiple members * the object allows 0 or more (hobbies *) hobbies can occur two or more times? The object must appear 0 or 1 times (rookie?) Rookies may or may not appear, but if they do, they can only appear once at most, and objects must appear in the specified order (watermelons, apples, bananas) indicating that watermelons, apples and bananas must appear, and 2.4 attribute definitions appear in this order.

The definition of attributes in DTD is as follows:

Among them, there are five types of attributes:

(1) CDATA

(2) ID

(3) IDREF/IDREFS

(4) Enumerated

(5) ENTITY/ENTITIES

The characteristics of attributes are as follows:

(1) # REQUIRED, indicating that this attribute must be given. Otherwise, an error will be reported.

(2) # IMPLIED, indicating that this attribute can be given or not.

(3) # FIXED value, indicating that this attribute must be given a fixed value

(4) Default value, which means that if this property has no value, it will be assigned a default value

For example, we want to add the address attribute to the child element of the student, and this attribute is required, as shown in the following example:

Zhou Xiaoxing 23 studies hard Lin Xiao 25 is a good student

At this time, the corresponding DTD file should also be updated, otherwise an error will be reported, as follows:

2.4.1 detailed explanation of attribute types

(1) attribute type-CDATA, indicating that the attribute value can be any character (including Chinese characters and numbers)

(2) attribute type-ID, indicating that the value of the attribute must be unique, but the value of the attribute cannot start with a number!

(3) attribute type-IDREF/IDREFS

The value of the-IDREF attribute points to a value of type ID declared elsewhere in the document

-IDREFS is the same as IDREF, but can have multiple references separated by spaces.

(4) attribute type-Enumerated, some values are defined in advance, and the value of the attribute must be within the range of the values listed.

(5) attribute type-ENTITY, entity

Entity definition:

-the entity is used to create an alias for a piece of content that can later be referenced in the XML document using an alias.

-in the DTD definition, a! ENTITY statement is used to define an entity.

-entities can be divided into two types: reference entities and parameter entities. Reference entities are applied by XML documents, while parameter entities are applied by the DTD file itself.

① reference entity:

Reference entities are mainly used in XML documents

The syntax format is as follows, and the definition of the reference entity is best placed at the end of the DTD file.

Citation method: & entity name; end with a semicolon, this reference will be directly transformed into entity content

Examples are as follows:

.... & copyright

② parameter entity:

The parameter entity is used by the DTD file itself

The syntax format is:

Reference method is:% entity name

For example:

Actual case of 3.DTD

The goal of learning DTD is:

(1) We are required to be able to read DTD documents.

(2) We can write the corresponding XML file according to the DTD given.

Let's take a look at a case. The following DTD file is taken from the DTD case in the W3School online tutorial. We should be able to read each line carefully.

Then we can write the following simplest XML file based on this DTD:

Here are the details 25 28.

Then we use Microsoft.XMLDOM to verify the XML and find that there are no errors. But pay attention to the coding.

A brief introduction to 1.XML File constraints and DTD

We write documents to constrain the writing specification of an XML document, which is called a XML constraint.

Commonly used constraint techniques are:

XML DTD

XML Schema

The basic concepts of DTD:

Document type definition document type definition

DTD files are generally used in conjunction with XML files, mainly to constrain XML files.

The XML file introduces the DTD file so that XML can customize the tags but is constrained by the DTD file. For example, the previous section uses XML to describe the information of a class. If we define a tag for each student, there is no grammatical error, but it is not semantic. How can students describe it in terms of area? At this point, we need to use the DTD file to constrain the XML.

Yang Guo male 20 100 1.1 DTD constraint Quick start case

Basic syntax:

We also take the class as an example and write the following DTD file, myClass.dtd:

The first line indicates that the root element is a class and has a student as a child element, with one or more child elements.

The second line indicates that the student's child elements are name, age, and introduction.

There are no child elements under the name, so # PCDATA means that you can put any text in the name.

Age and introduction are also similar.

Write the myClass.xml file and introduce the DTD file as follows:

Zhou Xiaoxing 23 studies hard Lin Xiao 25 is a good student

Written in introduction: SYSTEM, indicating that the current DTD file is local

If you write PUBLIC, it means that the introduced DTD file is from the network.

The DTD file introduced at this time has no effect. If we add child elements to the student element and open the XML file, the browser will still not report an error.

Zhou Xiaoxing 23 studies hard 100 square meters Lin Xiao 25 is a good student

We need to program to verify the correctness of the XML document.

Browsers above IE5 have built-in XML parsing tool: Microsoft.XMLDOM, developers can write JavaScript code, use this parsing tool to load XML files, and DTD verification of XML files.

We write myXmlTools.html to verify this XML, as follows:

/ / create xml document parser object var xmldoc = new ActiveXObject ("Microsoft.XMLDOM"); / / enable xml verification xmldoc.validateOnParse = "true"; / / load xml document, that is, specify which XML file to validate xmldoc.load ("myClass.xml"); [xss_clean] ln ("error message:" + xmldoc.parseError.reason+ ")

"); [xss_clean] ln (" error line number: "+ xmldoc.parseError.line)

Open the html file with an IE browser, and you can see the running result:

You can see that line 9 is the same line we added.

2.DTD details 2.1 statements and references to DTD documents

1. Internal DTD document

two。 External DTD document

There are two types of imported DTD documents:

(1) when the referenced DTD file is a local file, identify it with SYSTEM and write "the file path of DTD", as follows:

(2) if the referenced DTD file is a public file, use the PUBLIC logo as follows:

Such as the following example:

2.2 basic DTD syntax:

Where:

-ELEMENT is a keyword and cannot be modified.

-NAME represents the element name

-CONTENT is an element type and must be capitalized! The content of CONTENT can be written in three ways:

(1) EMPTY-- means that the element cannot contain child elements and text, but can have attributes.

(2) ANY-- means that the element can contain any element content defined in the DTD

(3) # PCDATA-- can contain any character data, but it cannot contain any child elements

2.3 combination type of DTD elements:

The DTD states as follows:

This DTD specifies that the family element can have one or more "people" as child elements, or 0 to more "home appliances" as child elements. The meaning of the plus sign "+" and the asterisk "*" is the same as that in the regular expression.

XML wrote:

With regard to combination types, the following modifiers are available:

The symbolic usage example shows that () is used to group elements (Gulong | Jin Yong) and (Wang Shuo | Yu Jie) into two groups | selecting one of the listed objects (man | woman) means that a man or woman must appear. At least one of them + the object must appear once or more times (member +) indicates that the member must appear. However, there can be multiple members * the object allows 0 or more (hobbies *) hobbies can occur two or more times? The object must appear 0 or 1 times (rookie?) Rookies may or may not appear, but if they do, they can only appear once at most, and objects must appear in the specified order (watermelons, apples, bananas) indicating that watermelons, apples and bananas must appear, and 2.4 attribute definitions appear in this order.

The definition of attributes in DTD is as follows:

Among them, there are five types of attributes:

(1) CDATA

(2) ID

(3) IDREF/IDREFS

(4) Enumerated

(5) ENTITY/ENTITIES

The characteristics of attributes are as follows:

(1) # REQUIRED, indicating that this attribute must be given. Otherwise, an error will be reported.

(2) # IMPLIED, indicating that this attribute can be given or not.

(3) # FIXED value, indicating that this attribute must be given a fixed value

(4) Default value, which means that if this property has no value, it will be assigned a default value

For example, we want to add the address attribute to the child element of the student, and this attribute is required, as shown in the following example:

Zhou Xiaoxing 23 studies hard Lin Xiao 25 is a good student

At this time, the corresponding DTD file should also be updated, otherwise an error will be reported, as follows:

2.4.1 detailed explanation of attribute types

(1) attribute type-CDATA, indicating that the attribute value can be any character (including Chinese characters and numbers)

(2) attribute type-ID, indicating that the value of the attribute must be unique, but the value of the attribute cannot start with a number!

(3) attribute type-IDREF/IDREFS

The value of the-IDREF attribute points to a value of type ID declared elsewhere in the document

-IDREFS is the same as IDREF, but can have multiple references separated by spaces.

(4) attribute type-Enumerated, some values are defined in advance, and the value of the attribute must be within the range of the values listed.

(5) attribute type-ENTITY, entity

Entity definition:

-the entity is used to create an alias for a piece of content that can later be referenced in the XML document using an alias.

-in the DTD definition, a! ENTITY statement is used to define an entity.

-entities can be divided into two types: reference entities and parameter entities. Reference entities are applied by XML documents, while parameter entities are applied by the DTD file itself.

① reference entity:

Reference entities are mainly used in XML documents

The syntax format is as follows, and the definition of the reference entity is best placed at the end of the DTD file.

Citation method: & entity name; end with a semicolon, this reference will be directly transformed into entity content

Examples are as follows:

.... & copyright

② parameter entity:

The parameter entity is used by the DTD file itself

The syntax format is:

Reference method is:% entity name

For example:

Actual case of 3.DTD

The goal of learning DTD is:

(1) We are required to be able to read DTD documents.

(2) We can write the corresponding XML file according to the DTD given.

Let's take a look at a case. The following DTD file is taken from the DTD case in the W3School online tutorial. We should be able to read each line carefully.

Then we can write the following simplest XML file based on this DTD:

Here are the details 25 28.

Then we use Microsoft.XMLDOM to verify the XML and find that there are no errors. But pay attention to the coding.

The above is all the contents of the article "sample Analysis of XML File constraint DTD". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report