So far, XML is still a very new technology, so most of the published XML-based books lack the introduction to development practices. After reading these books, many of my friends still cannot figure out what XML can do for a long time due to lack of practice. Can they use XML for our daily development. I decided to give you a simple example to demonstrate the XML development process. This post is the first one in a series of posts in the XML Development tutorial at the request of my friends.
This example is from the easy-to-use XML advanced by the e-Industry Publishing House. The original example only contains the DTD definition and XML file, I have modified the example and added a lot of content to meet our requirements.
You can maintain this example together and add new functions to demonstrate all aspects of XML development. In the end, I hope to make it a complete and easy-to-understand demo of XML development.
First, let's talk about my requirements for this software:
I have many books. I want to input information about these books into my computer. For ease of processing, I hope the information can be saved in XML format. I also want to define my own DTD or Schema so that when I save information in an XML file, the file format can be customized. Because Mozilla is installed with a browser that supports XML, I can directly use Mozilla to view my saved information as long as an XSL style sheet is provided. To demonstrate the relationship between XML and the real world, I decided to store the information in the database, use JSP to obtain the information in the database, and complete XSLT conversion on the server side, in this way, I can use any browser to view my saved information. Finally, because I am a Chinese, I hope to be able to use Chinese freely in this software.
This is all my requirements for this software. All the requirements are clear and specific.
To meet these requirements, we will do so in several steps.
1. Create a DTD and XML file and perform DTD verification.
2. Create a Schema and XML file and verify the Schema.
3. Create an XSL style sheet to browse XML files in Mozilla.
4. Store information to the database, use JSP to obtain information, and complete XSLT conversion on the server side.
5. Solve Chinese processing problems in the software.
Let me talk about the first step today.
First, define a DTD.
<? Xml version = "1.0" encoding = "UTF-8"?>
<! ELEMENT library (book *)>
<! ELEMENT book (title, author, publisher, cover, category, isbn, rating, comments?)>
<! ELEMENT title (# PCDATA)>
<! ELEMENT author (# PCDATA)>
<! ELEMENT publisher (# PCDATA)>
<! ELEMENT cover EMPTY>
<! ATTLIST cover type (hardback | paperback) "paperback">
<! ELEMENT category EMPTY>
<! ATTLIST category class (entertainment | technology) "entertainment">
<! ELEMENT isbn (# PCDATA)>
<! ELEMENT rating EMPTY>
<! ATTLIST rating number (1 | 2 | 3 | 4 | 5) "3">
<! ELEMENT comments (# PCDATA)>
Save the file as library. dtd.
This DTD is clear at a glance. The library element can contain any number of book elements. The book element can contain a title element, an author element, a publisher element, a cover element, a category element, An isbn element, a rating element, and an optional comments element. The cover element can have a type attribute. The category element can have a class attribute. The rating element can have a number attribute. The meanings of each element are as follows:
Library: library. Book: book. Title: title. Author: author. Publisher: the publisher. Cover: cover type (hardcover/lite ). Category: category. Isbn: Book Number. Rating: level. Comments: comments.
With this DTD, you can write an XML file.
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE library SYSTEM "library. dtd">
<Library>
<! -- Begin book entry -->
<Book>
<Title> XML: In Record Time </title>
<Author> Natanya Pitts </author>
<Publisher> SYBEX </publisher>
<Cover type = "paperback"/>
<Category class = "technology"/>
<Isbn> 7-5053-5550-3 </isbn>
<Rating number = "1"/>
<Comments>
A very good book on XML.
</Comments>
</Book>
<Book>
<Title> Principles of Web Design </title>
<Author> Joel Sklar </author>
<Publisher> COURSE </publisher>
<Cover type = "paperback"/>
<Category class = "technology"/>
<Isbn> 7-111-08237-0 </isbn>
<Rating number = "1"/>
<Comments>
A very good book on WEB design.
</Comments>
</Book>
<! -- End book entry -->
</Library>
Save the file as books. xml.
The syntax defined in Library. DTD in this XML file contains two books. Note: To enable the XML text in the post to be displayed, I add a space after each tag. Do not use it directly.
Use nsgmls to test whether the XML file format is correct:
Nsgmls-WXML-wno-valid-s books. xml
If there is no output, the format is correct, and then you can perform DTD Verification:
Nsgmls-WXML-s books. xml
If no output is available, the DTD verification is successful.
Alternatively, you can use xerces for DTD verification. For more information, see my post: Describes schema verification and DTD verification methods.
The next step is to provide a schema corresponding to this DTD and write an XML file using the schema. Because I believe that schema is a better way to define XML syntax than DTD, I will focus on using schema in my examples later.
In the previous example, we created a DTD and used this DTD to write an XML file. The following describes how to create a schema equivalent to the DTD and use the schema to write XML files.
First, define a schema.
<? XML version = "1.0" encoding = "UTF-8"?>
<XSD: schema xmlns: XSD = "http://www.w3.org/2000/10/XMLSchema"
Targetnamespace = "http: // localhost"
Xmlns = "http: // localhost"
ElementFormDefault = "qualified">
<Xsd: element name = "library">
<Xsd: complexType>
<Xsd: sequence>
<Xsd: element name = "book" minOccurs = "0" maxOccurs = "unbounded">
<Xsd: complexType>
<Xsd: sequence>
<Xsd: element name = "title" type = "xsd: string" minOccurs = "1" maxOccurs = "1"/>
<Xsd: element name = "author" type = "xsd: string" minOccurs = "1" maxOccurs = "1"/>
<Xsd: element name = "publisher" type = "xsd: string" minOccurs = "1" maxOccurs = "1"/>
<Xsd: element name = "cover" minOccurs = "1" maxOccurs = "1">
<Xsd: complexType>
<Xsd: attribute name = "type">
<Xsd: simpleType>
<Xsd: restriction base = "xsd: string">
<Xsd: enumeration value = "hardback"/>
<Xsd: enumeration value = "paperback"/>
</Xsd: restriction>
</Xsd: simpleType>
</Xsd: attribute>
</Xsd: complexType>
</Xsd: element>
<Xsd: element name = "category" minOccurs = "1" maxOccurs = "1">
<Xsd: complexType>
<Xsd: attribute name = "class">
<Xsd: simpleType>
<Xsd: restriction base = "xsd: string">
<Xsd: enumeration value = "entertainment"/>
<Xsd: enumeration value = "technology"/>
</Xsd: restriction>
</Xsd: simpleType>
</Xsd: attribute>
</Xsd: complexType>
</Xsd: element>
<Xsd: element name = "isbn" type = "xsd: string" minOccurs = "1" maxOccurs = "1"/>
<Xsd: element name = "rating" minOccurs = "1" maxOccurs = "1">
<Xsd: complexType>
<Xsd: attribute name = "number">
<Xsd: simpleType>
<Xsd: restriction base = "xsd: string">
<Xsd: enumeration value = "1"/>
<Xsd: enumeration value = "2"/>
<Xsd: enumeration value = "3"/>
<Xsd: enumeration value = "4"/>
<Xsd: enumeration value = "5"/>
</Xsd: restriction>
</Xsd: simpleType>
</Xsd: attribute>
</Xsd: complexType>
</Xsd: element>
<Xsd: element name = "comments" type = "xsd: string" minOccurs = "0" maxOccurs = "1"/>
</Xsd: sequence>
</Xsd: complexType>
</Xsd: element>
</Xsd: sequence>
</Xsd: complexType>
</Xsd: element>
</Xsd: schema>
Save the file as library. xsd.
This Schema provides exactly the same functionality as the last created DTD. With this Schema, you can write an XML file.
<? Xml version = "1.0" encoding = "UTF-8"?>
<Library xmlns = "http: // localhost"
Xmlns: xsd = "http://www.w3.org/2000/10/XMLSchema-instance"
Xsd: schemaLocation = "http: // localhost library. xsd">
<! -- Begin book entry -->
<Book>
<Title> XML: In Record Time _~. _ </Title>
<Author> Natanya Pitts </author>
<Publisher> SYBEX </publisher>
<Cover type = "paperback"/>
<Category class = "technology"/>
<Isbn> 7-5053-5550-3 </isbn>
<Rating number = "1"/>
<Comments>
A very good book on XML.
</Comments>
</Book>
<Book>
<Title> Principles of Web Design </title>
<Author> Joel Sklar </author>
<Publisher> COURSE </publisher>
<Cover type = "paperback"/>
<Category class = "technology"/>
<Isbn> 7-111-08237-0 </isbn>
<Rating number = "1"/>
<Comments>
A very good book on WEB design.
</Comments>
</Book>
<! -- End book entry -->
</Library>
Save the file as books-schema.xml.
The syntax defined in library. xsd in this XML file contains two books.
Use XSV for Schema Verification:
Xsv books-schema.xml
If no errors occur, Schema verification is successful.
Alternatively, you can use Xerces for Schema verification. For more information, see my post on Schema verification and DTD verification.
The latest Schema version is recommended from, but most of the available tools including Xerces currently only support the 2000-10-24 version. So to use these tools for learning, please stay on this old version for the moment. The latest XSV version supports the version.
Regarding Schema, Mr. Roger L. Costello wrote some excellent tutorials, including a large number of examples for reference. In: http://www.xfront.com
The next step is to provide an XSL style sheet for the compiled XML file to be displayed in Mozilla.