XML & amp; DTD & amp; XML Schema Learning

Source: Internet
Author: User

XML & DTD & XML Schema Learning
XML (eXtensible Markup Language) eXtensible Markup Language. Xml is particularly important in web service programming. It can be used as a carrier for data transmission during network transmission. Xml, as a metadata language, can be used to mark and define data types. It is a metadata language that allows users to define their own Markup languages. It is ideal for network transmission and provides a unified way to describe and exchange structured data independent of applications or vendors. Allows interaction between heterogeneous languages and platforms. XML document definition methods include document type definition (DTD) and XML Schema. Document Type Definition (DTD) defines the overall structure of the Document and the syntax of the Document (the following example is used for understanding). It is widely used and supports a wide range of tools. XML Schema is more powerful and used to define management information and other more powerful and richer features. XML can declare content in a more fine-grained manner and define constraints to facilitate more meaningful transfer of content across multiple platforms. It provides a format for describing structured data, simplifies data exchange and representation in the network, separates code, data, and representation, and serves as a standard format for data exchange, therefore, it is often called Smart Data Documentation. The DTD can be compared to the shovel, and the XML Schema can be called an excavator with more powerful functions. Although XML is called a markup language, Unlike HTML (Hypertext Markup Language), XML adopts a more strict format, tags must be closed, and display and content are separated, XML describes the content and semantics of a document, rather than how the document is displayed. HTML has fixed tags, and the display and content are the same. You cannot create your own tags. XML is scalable, and the content and display are separated. Each element becomes a pair, it ends at the beginning, and the nesting relationship of XML elements must be correct (the starting mark in nesting must end first, and the starting mark must end later ), each XML document contains only one Root Element. The Root Element contains all other elements. XML documents that conform to the XML syntax are called well form when they are not verified by DTD or XML Schema. If the xml documents that conform to the XML syntax are verified by DTD or XML Schema, this xml document is called a Valid (Valid. The following uses the xml of a student register as an example to copy code 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <student roster> 3 <student ID = "A1"> 4 <Name> CIACs </Name> 5 <gender> male </gender> 6 <age> 22 </age> 7 </student> 8 9 <student ID = "A2"> 10 <Name> zhihao </Name> 11 <gender> male </gender> 12 <age> 23 </age> 13 </student> 14 </Student Register> copy xml documents with good code format, in the output window of XMLSpy, the first line of the following result xml must be <? Xml version = "1.0"?> Process the command, and "<? There cannot be blank between xml, xml elements are strictly case sensitive, the document encoding format defaults to "UTF-8", version only 1.0. The preceding xml document can only be a well-formatted xml document, not a valid (Vaild) xml document. We can verify it in two ways. The first is to use DTD to verify and copy code 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! DOCTYPE student register [3 <! ELEMENT student roster (student +)> 4 <! ELEMENT student (name, gender, age)> 5 <! ELEMENT name (# PCDATA)> 6 <! ELEMENT gender (# PCDATA)> 7 <! ELEMENT age (# PCDATA)> 8 <! ATTLIST student ID # REQUIRED> 9 <! ENTITY sex "male"> 10]> 11 12 <Student Register> 13 <student ID = "A1"> 14 <Name> CIACs </Name> 15 <gender> & sex; </gender> 16 <age> 22 </age> 17 </student> 18 19 <student ID = "A2"> 20 <Name> zhihao </Name> 21 <gender> & sex; </gender> 22 <age> 23 </age> 23 </student> 24 </Student Register> copy the Code if it passes verification, in the XMLSpy output window, the following results are displayed. Otherwise, verification fails even if one space is added. Here I write the dtd verification to xml. Of course you can also write it to another file with the suffix ". dtd, and then associate it with the xml document to be verified. The syntax is as follows: 1 <! DOCTYPE root element name SYSTEM "*. dtd"> the ID value here seems to start with a character, if only a number is not verified. The Gender in the student information. I define it as an entity and reference its value through the entity. Note that the syntax of Object Reference is "& entity name ;". The following uses the XML Schema method to verify that the xml document to be verified is copied to code 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <Student Register xmlns: xsi =" http://www.w3.org/2001/XMLSchema -Instance "xsi: noNamespaceSchemaLocation =" Student Register. xsd "> 3 <student ID =" A1 "> 4 <Name> CIACs </Name> 5 <gender> male </gender> 6 <age> 22 </age> 7 </student> 8 9 <student ID = "A2"> 10 <Name> zhihao </Name> 11 <gender> male </gender> 12 <age> 23 </age> 13 </student> 14 </Student Register> copy code XML Schema Verification Document Copy code 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <xs: schema xmlns: xs =" http://www.w3.org/2001/XMLSchema "ElementFormDefault =" qualified "attributeFormDefault =" unqualified "> 3 <xs: element name =" Student Register "> 4 <xs: complexType> 5 <xs: sequence minOccurs = "1" maxOccurs = "unbounded"> 6 <xs: element ref = "student"/> 7 </xs: sequence> 8 </xs: complexType> 9 </xs: element> 10 <xs: element name = "student"> 11 <xs: complexType> 12 <xs: sequence> 13 <xs: element name = "name" type = "xs: string"/> 14 <xs: element name = "gender"> 15 <xs: simpleType> 16 <xs: restriction base = "xs: string"> 17 <xs: enumeration value = "male"/> 18 <xs: enumeration value = "female"/> 19 </xs: restriction> 20 </xs: simpleType> 21 </xs: element> 22 <xs: element name = "Age"> 23 <xs: simpleType> 24 <xs: restriction base = "xs: integer"> 25 <xs: minExclusive value = "0"/> 26 <xs: maxExclusive value = "120"/> 27 </xs: restriction> 28 </xs: simpleType> 29 </xs: element> 30 </xs: sequence> 31 <xs: attribute name = "student ID" type = "xs: string "use =" required "/> 32 </xs: complexType> 33 </xs: element> 34 </xs: schema> the xml document to be verified adds the following information to the root element start tag to associate XML Schema document 1 xmlns: xsi =" http://www.w3.org/2001/XMLSchema -Instance "xsi: noNamespaceSchemaLocation =". xsd "and above are all stored in the same path, so simply reference the file name. From the above two verification methods, we can clearly see the difference between DTD and XML Schema verification. The two are also verified in xml documents, XML Schema provides more powerful functions and finer-grained data types than DTD, and it can also customize data types, which are xml files, however, the dtd syntax is different from the xml syntax. Although the Schema is larger than the dtd in terms of code, you will prefer Schema after learning the Schema. Learning xml and its verification methods are important for later learning web service programming.

Related Article

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.