Home  >  Article  >  Backend Development  >  XML easy learning manual

XML easy learning manual

黄舟
黄舟Original
2016-12-23 11:16:262336browse

(1): Quick introduction to XML

 Foreword
 XML is becoming more and more popular, and basic tutorials about XML can be found everywhere on the Internet. However, a lot of concepts and terms are often daunting. Many friends asked me: What is the use of XML? Do we need to learn it? I want to write a more comprehensive introduction article based on my personal learning process and experience. First of all, there are two points that need to be affirmed:
First: XML is definitely the future development trend. Whether you are a web designer or a network programmer, you should learn and understand it in time. Waiting will only make you lose the opportunity;
Second: There will definitely be many new concepts in new knowledge. Only by trying to understand and accept them can you improve. Don't be afraid and run away, after all we are still young.
 Outline
 This article is divided into five parts. They are XML quick introduction, XML concepts, XML terminology, XML implementation, and XML example analysis. The final appendix introduces XML-related resources. The author stands from the perspective of ordinary web designers and uses plain and vivid language to tell you all aspects of XML, helping you to lift the mystery of XML and quickly enter the new field of XML.
 Chapter 1: Quick Start with XML
 1. What is XML?
 2. Is XML a new concept?
 3. What are the benefits of using XML?
 4. Is XML difficult to learn?
  5. The difference between XML and HTML
6. The strict format of XML
7. More about XML

1. What is XML?

 This is often the first question, and often you will not understand it on the first question, because most textbooks answer this:
 XML is the abbreviation of Extensible Markup Language, an extensible markup language.
 This is the standard definition. So what is a markup language and why is it called extensibility? It's already a bit confusing. I think it would be better if we understand it this way:
You are already very familiar with HTML. It is a markup language. Do you remember its full name: "Hypertext Markup Language". Understood? At the same time, there are many tags in HTML, such as

, , etc., which are all standardized and defined in HTML 4.0, and XML allows you to create such tags yourself, so it is called extensibility.
 There are a few confusing concepts to remind everyone:
 1.XML is not a markup language. It is just a metalanguage used to create markup languages ​​(such as HTML). God, I'm confused again! It doesn't matter, you just need to know this: XML is different from HTML. Its uses are much wider than HTML, which we will introduce in detail later.
 2.XML is not a replacement for HTML. XML is not an upgrade of HTML, it is just a supplement to HTML, extending more functions to HTML. We will continue to use HTML for a long time to come. (But it is worth noting that XHTML, the upgraded version of HTML, is indeed moving closer to adapting to XML.)
 3. You cannot use XML to write web pages directly. Even if it contains XML data, it still needs to be converted into HTML format before it can be displayed on the browser.
  The following is an XML sample document (Example 1), used to represent the information of this article:
 
 
 XML Quick Start
ajie
  ajie@aolhoo.com
 The code gives you a preliminary understanding of XML, but it cannot achieve any specific application;
 2. Statements like , <author> are tags created by yourself. They are different from HTML tags, such as The <title> here means the article title, and the <title> in HTML means the page title. <br><br> 2. Is XML a new concept? <br> No. XML is derived from SGML, a markup language standard that is earlier than HTML. <p> About SGML, let’s take a brief look at it. You only need to have a general idea. </p> The full name of SGML is "Standard Generalized Markup Language". You can tell just by looking at the name: it is a standard for markup languages, which means that all markup languages ​​are developed in accordance with SGML, including HTML, of course. SGML has a wide coverage. All files with a certain format belong to SGML, such as reports, music scores, etc. HTML is the most common file format of SGML on the Internet. Therefore, people jokingly call SGML the "mother" of HTML. <p> XML is a simplified version of SGML, except that the complex and uncommon parts are omitted. (Oh, I understand! It is the second "mother" of HTML. No wonder it is more powerful than HTML.) Like SGML, XML can also be applied in various fields such as finance and scientific research. What we are talking about here is just the application of XML in the web. Just the application. <br> At this point, you should have a little understanding: XML is used to create and define a markup language similar to HTML, and then use this markup language to display information. <br><br>  3. What are the benefits of using XML? <br></p> <p> With HTML, why do we need to use XML? <br> Because network applications are becoming more and more widespread, it is no longer enough to rely on a single file type of HTML to handle ever-changing documents and data. Moreover, the grammar of HTML itself is very loose, which seriously affects the transmission and sharing of network information. (Think about how many designers’ brain cells have been damaged by browser compatibility issues.) People have already begun to explore ways to meet the needs of various applications on the Internet. It is possible to use SGML, but SGML is too large and complicated to program, so I finally chose the "weight-loss" SGML---XML as the data transmission and interaction tool for next-generation web applications. <br> What are the benefits of using XML? Let’s look at the description from the w3c organization (XML standard setter): <br> XML makes using the SGML language on the Internet more "simple and direct": It simplifies the process of defining file types, simplifies the process of programming and processing SGML files, and simplifies the process of using SGML files on the Internet. Delivery and sharing on the Web. <br> 1.XML can be widely used anywhere on the web; <br> 2.XML can meet the needs of network applications; <br> 3. Using XML will make programming easier; <br> 4.XML is easy to learn and create; <br> 5.XML code Will be clear and easy to read and understand; <br> Still a bit abstract. Let us slowly experience the powerful advantages of XML in the following example tutorials! <br> 4. Is XML difficult to learn? <br> If you are interested in learning XML, you can’t help but ask: Is XML difficult? What kind of foundation is needed to learn XML? <br> XML is very simple and easy to learn. If you are familiar with HTML, you will find that its documents are very similar to HTML. Look at the same sample document (Example 1): <br> <?xml version="1.0"?><br> <myfile><br> <title>XML Quick Start
 ajie
  ajie@aolhoo.com
 OK is an XML declaration, indicating that the document follows the XML version 1.0 specification.
 The second line defines the first element (element) in the document, also called the root element: . This is similar to the opening tag in HTML. Note that this name is defined arbitrarily by yourself.
  Four sub-elements are defined below: title, author, email, and date. Indicate the title, author, email address and date of the article respectively. Of course, you can define these tags in Chinese, which makes them easier to understand:
 
 

  XML is easy to learn Manual
 ajie
 ajie@aolhoo.com
 20010115
 
This is an XML document. Any netizen who knows HTML can directly write such a simple XML document.
 In addition, learning XML must also master a page scripting language, the common ones are javascript and VB script. Because XML data uses script to implement calling and interaction in HTML. Let’s look at the simplest example (Example 2):
 1. Save the following code as myfile.htm
 
 
 
 Calling XML data in HTML
 
 
 Title :
 

  Author:
 < ;/span>

  Email:
 

 Date: b>
 

 
 
  2. Save the following code as myfile. version="1.0" encoding="GB2312"?>
 
 XML Easy Learning Manual
ajie
  ajie@aolhoo.com
  20010115
  

  3. Put them in the same directory , open it with IE5 or above browser, and you can see the effect. Click here to see an example.
 Learn and master a script, and you will truly unleash the extremely powerful capabilities of XML.

5. The difference between XML and HTML

Both XML and HTML come from SGML. They both contain tags and have similar syntax. The biggest difference between HTML and XML is that HTML is a stereotyped markup language that uses inherent tags. To describe and display the content of the web page. For example,

represents the first line of title and has a fixed size. In contrast, XML has no fixed tags. XML cannot describe the specific appearance and content of a web page. It only describes the data form and structure of the content.
This is a qualitative difference: web pages mix data and display, while XML separates data and display.
  Let’s look at the example above. In myfile.htm, we only care about the way the page is displayed. We can design different interfaces and layout the page in different ways, but the data is stored in myfile.xml and does not require any changes. .
 (If you are a programmer, you will be surprised to find that this is very similar to the idea of ​​modular object-oriented programming! In fact, isn’t a web page a program?)
 It is this difference that makes XML important in network applications and information sharing. Convenient, efficient and scalable. Therefore, we believe that XML, as an advanced data processing method, will bring the network to a new realm.

 6. The strict format of XML

Drawing lessons from the loose format of HTML, XML has insisted on implementing "good format" from the beginning.
 Let’s first look at some statements in HTML, which can be found everywhere in HTML:
 1. ;td>sample
 4.samplar
 In XML documents, the syntax of the above statements is wrong. Because:
 1. All tags must have a corresponding closing tag;
 2. All XML tags must be reasonably nested;
 3. All XML tags are case-sensitive;
 4. All tag attributes must Surrounded by "";
So the correct way to write the above statement in XML is
1.

sample


  2.sample> ;
  3.
  4.samplar
  In addition, XML tags must follow the following naming rules:
  1. The name can contain Letters, numbers and other letters;
 2. The name cannot start with numbers or "_" (underscore);
 3. The name cannot start with the letters xml (or XML or Xml..);
 4. The name cannot contain spaces.
Any error in the XML document will result in the same result: the web page cannot be displayed. Browser developers have reached an agreement to implement strict and picky parsing of XML, and any small errors will be reported. You can modify the myfile.xml above, for example, change to , and then open myfile.xml directly with IE5, you will get an error message page:
 
 
 XML Easy Learning Manual
  ajie
 ;
 , the relationship and difference between SGML;
 3. Simple application of XML.
Congratulations! You no longer know anything about XML, and you are already at the forefront of network technology. The whole learning process does not seem to be difficult :)
If you are more interested in XML and want to know more about the details of XML and other practical application technologies, please continue to browse our next chapter: The concept of XML.

Chapter 2 XML Concepts
Introduction
After the quick start in Chapter 1, you already know that XML is a language that allows you to create your own tags. It can separate data and formats from web pages, and it can store data. And the characteristics of sharing data make XML omnipotent. If you want to learn XML in depth and systematically master the ins and outs of XML, then we must first return to the concept of XML. XML (Extensible Markup Language), an extensible markup language. "Extensibility" "Identity" "Language". Each word clearly points out the important features and functions of XML. Let’s analyze it carefully:
  1. Extensibility
2. Identification
3. Language
4. Structure
5. Meta data
6. Display
7. DOM

1. Extensibility---using XML, you can Create your own tags for your documents.

 The first word of XML is "expansibility", which is why XML has powerful functions and flexibility.
 In HTML, there are many fixed tags that we must remember and then use. You cannot use tags that are not in the HTML specification. In XML, you can create any markup you need. You can give full play to your imagination and give your documents some memorable tag names. For example, if your document contains some game guides, you can create a tag named , and then create , and other tags under according to the game category. You can create any number of markers as long as they are clear and easy to understand.
You may not be comfortable with it at first, because when we learn HTML, there are fixed tags that can be learned and used directly; (many people, including myself, build their own web pages while analyzing other people's codes and logos), and XML does not have any tags to learn, and few documents have exactly the same tags. what should we do? Haha, if not, just create it yourself. Once you actually start writing XML documents, you'll find that it's fun to create new tags as you like. You can create your own unique markup and even create your own HTML language.
 Extensibility gives you more choices and powerful capabilities, but it also creates a problem: you must learn to plan. You need to understand your own document, know what parts it consists of, the relationship between them and how to identify them.
 One thing to note about establishing a logo is that the logo describes the type or characteristics of the data, such as width , age , name , etc., rather than the content of the data, such as: ,,, these are useless tags. If you have studied databases, you can understand it this way, an identifier is a field name.

  2. Identification---Using XML you can identify elements in the document.

 The second word of XML is "identification", which shows that the purpose of XML is to identify elements in the document.
 Whether you are using HTML or XML, the essence of tags is to facilitate understanding. If there is no tag, your document will appear to the computer as just a long string, and every word will look the same, with no emphasis.
 Through tags, your document will be easier to read and understand. You can divide it into paragraphs and list titles. In XML, you can take advantage of its extensibility to create more appropriate tags for documents.
  However, there is one thing to remind everyone: the logo is only used to identify information, it does not convey information itself. For example, this HTML code:
 frist step
 Here means bold. It is only used to indicate that the "frist step" character is displayed in bold. itself does not contain You can't see any actual information on the page, it's the "frist step" that really conveys the information.

  3. Language---When using XML, you have to follow a specific syntax to identify your documents.

 The third word of XML is "language". This shows that XML as a language must follow certain rules. Although XML's extensibility allows you to create new identities, it still must follow a specific structure, syntax, and clear definitions.
 In the field of computers, language often refers to a "programming language", which is used to program to implement some functions and applications. However, not all "languages" are used for programming. XML is just a language used to define identification and describe information. language.
Next let’s take an in-depth understanding of the basic principles of XML application. It may be boring, but the overall understanding is very important. You can go through it quickly first to have a vague concept in your mind. The specific essence needs to be learned slowly in practice. understand.

 IV. Structure---XML promotes document structure, and all information is arranged according to a certain relationship.

  "Structure" sounds too abstract. We understand it this way. Structure is to establish a framework for your document, just like writing an outline before writing an article. Structure makes your document look less disorganized and each part is closely connected to form a whole.
 There are two principles of structuring:
 1. Each part (each element) is related to other elements. The associated series form the structure.
 2. The meaning of the logo itself is separated from the information it describes.
  Let’s look at a simple example to help understand:
  
  
  XML Easy Learning Manual
   < ;chapter>Quick Start with XML
                                             die in in XML > ;Extensibility
  Identity
  
 Clear:
  
  
   
    ...
   

  

  

  A document like the above Structure, also known as For a "document tree", the trunk is the parent element, such as , and the branches and pages are child elements, such as and .

 5. Meta data (Metadata)---Professional XML users will use meta data to work.

 In HTML, we know that we can use meta tags to define keywords, introductions, etc. of web pages. These tags will not be displayed on the web page, but can be searched by search engines and affect the order of search results.
 XML deepens and expands this principle. With XML, you can describe where your information is. You can verify the information through meta, perform searches, force display, or process other data.

 The following are some uses of XML metadata in practical applications:

 1. Digital signatures can be verified to make online business submissions valid.

 2. Can be easily indexed and searched more effectively.

 3. Data can be transferred between different languages.
 The W3C organization is studying a metadata processing method called RDF (Resource Description Framework), which can automatically exchange information. W3C claims that using RDF combined with digital signatures will enable "authentic and credible" e-commerce to exist on the network.

 VI. Display

  XML alone cannot display pages. We use formatting technology, such as CSS or XSL, to display documents created by XML tags.
We mentioned in Chapter 1 that XML separates data and format. The XML document itself does not know how to display it, and there must be auxiliary files to help achieve it. (XML cancels all tags, including font, color, p and other style definition tags, so XML uses a method similar to CSS in DHTML to define document styles.), the file type used to set the display style in XML There are:

 1. It itself is also based on XML language. Using XSL, you can flexibly set the document display style, and the document will automatically adapt to any browser and PDA (handheld computer).

 XSL can also convert XML into HTML, so that old browsers can also browse XML documents.

 2.CSS

 We are all familiar with CSS. Its full name is Cascading Style Sheets, which is currently the main method used to display XML documents on browsers.
 3.Behaviors
 Behaviors have not yet become a standard. It is a unique feature of Microsoft's IE browser. You can use it to set some interesting actions on XML tags. If you are interested, you can click here to see an example

 7.DOM

 The full name of DOM is document object model. What is DOM used for? Assuming that your document is treated as a separate object, DOM is the standard for how to operate and control this object using HTML or XML.
 The object-oriented thinking method has become very popular. In programming languages ​​​​(such as java, js), object-oriented programming ideas are used. In XML, the web page is to be operated and controlled as an object. We can create our own objects and templates. To communicate with objects and how to command objects, you need to use API. The full name of API is Application Programming Interface, which is the rule for accessing and operating objects. DOM is an API that describes the rules of HTML/XML document objects in detail. It specifies the naming convention, program model, communication rules, etc. of HTML/XML document objects. In an XML document, we can think of each identification element as an object --- it has its own name and attributes.
  XML creates identifiers, and the role of DOM is to tell script how to operate and display these identifiers in the browser window
  Above we have briefly talked about some basic principles of XML, let’s take a look at the relationship between them and what they are How it works, first look at this picture:

XML easy learning manual

1.XML description data type. For example: "King lear" is a title element;
 2. CSS stores and controls the display style of the element. For example: the title will be displayed in 18pt font
 3.script script controls how the element behaves. For example: when a title element is "out of stock", it will be displayed in red.
 4.DOM provides a common platform for the communication of scripts and objects, and displays the results in the browser window.
 If there is an error in any part, you will not get the correct result.
 Okay, seeing this, we already have an overall general idea of ​​how XML works. Through the study of this chapter, we may feel that XML seems to be more biased towards data processing and is more convenient for programmers to learn. The actual situation is also the same. The purpose of XML design is to conveniently share and interact data. In the next chapter, we will systematically understand various terms about XML. You are welcome to continue browsing.

Chapter 3 XML Terminology
Outline:
Introduction
1. Terms related to XML documents
2. Terms related to DTD
Introduction
The most troublesome thing for beginners to learn XML is that there are a lot of new terminology concepts to understand. Since XML itself is also a brand-new technology, it is constantly developing and changing. Organizations and major network companies (Microsoft, IBM, SUN, etc.) are constantly introducing their own insights and standards, so it is not surprising that new concepts are flying everywhere. . There is no authoritative institution or organization in China to officially name these terms. Most of the Chinese textbooks you see about XML are translated based on the author's own understanding. Some are correct and some are wrong, which further hinders the development of XML. Our understanding and learning of these concepts.
 The explanations of XML terms you will see below are also the author’s own understanding and translation. Ajie is based on the XML1.0 standard specification released by the W3C organization and related official documentation. It can be ensured that these understandings are basically correct, or at least not wrong. If you want to read and understand further, I have listed the sources and links to relevant resources at the end of this article, which you can access directly. Okay, let’s get to the point:

 1. Terms related to XML documents

 What is an XML document? You know the HTML source code file? An XML document is an XML source code file written with XML tags. XML documents are also ASCII plain text files that you can create and modify using Notepad. The suffix name of XML documents is .XML, for example, myfile.xml. You can also directly open the .xml file with IE5.0 or above browsers, but what you see is the "XML original code" and the page content will not be displayed. You can try saving the following code as myfile.xml:
 
 
 XML Easy Learning Manual
  ajie
  ajie@aolhoo.com
  20010115
  

  The XML document contains three parts: 1 . An XML document declaration;
  2. A definition of a document type;
  3. Content created with XML tags.
  Example:
 
 
 
 
 QUICK START OF XML< ;/title><br> <author>ajie</author><br>  

 ……
  

  The first line It is the declaration of an XML document. The second line indicates that this document uses filelist.dtd to define the document type. The third line and below is the main part of the content.
 Let’s learn about the relevant terms in XML documents:
 1.Element (element):
 We already know the element in HTML. It is the smallest unit that makes up an HTML document, and it is the same in XML. An element is defined by a tag, including the start and end tags and the content inside, like this: ajie
 The only difference is that in HTML, the tag is fixed, while in XML , the logo needs to be created by yourself.
 2.Tag (logo)
 Tag is used to define elements. In XML, tags must appear in pairs, surrounding the data. The name of the identifier is the same as the name of the element. For example, such an element:
 ajie
 where is the identifier.
 3.Attribute:
 What is an attribute? Look at this HTML code:word. Among them, color is one of the attributes of font.
 Attributes are further descriptions and explanations of the logo. A logo can have multiple attributes, such as the font attribute and size. Attributes in XML are the same as attributes in HTML. Each attribute has its own name and value. The attribute is part of the identifier. Example:
 ajie
  Attributes in XML are also defined by yourself. We recommend that you try not to use attributes and change attributes into sub-elements. For example, the above code can be changed to Like this:
  ajie
  female
  

  The reason is that attributes are not easy to expand and be manipulated by programs.
 4.Declaration (Declaration)
  There is an XML declaration in the first line of all XML documents. This declaration indicates that this document is an XML document and which XML version specification it follows. An XML declaration statement looks like this:
 
 5.DTD (Document Type Definition)
  DTD is used to define elements, attributes and relationships between elements in XML documents.
 The DTD file can be used to check whether the structure of the XML document is correct. But creating an XML document does not necessarily require a DTD file. Detailed descriptions of DTD files will be listed separately below.
 6.Well-formed XML
 A document that abides by XML syntax rules and XML specifications is called "well-formed". If all your markup strictly adheres to the XML specification, then your XML document does not necessarily need a DTD file to define it.
A well-formed document must start with an XML declaration, for example:
It is currently 1.0; secondly, it means that the document is "independent" and it does not require a DTD file to verify whether the identification in it is valid; thirdly, it is necessary to explain the language encoding used in the document. The default is UTF-8. If you use Chinese, you need to set it to GB2312.
 A well-formatted XML document must have a root element, which is the first element created immediately after the declaration. Other elements are child elements of this root element and belong to a group of root elements.
The content of a well-formed XML document must comply with XML syntax when written. (We will explain XML syntax in detail in the next chapter) 7. Valid XML (valid XML) An XML document that abides by XML syntax rules and complies with the corresponding DTD file specifications is called a valid XML document. Note that we compare "Well-formed XML" and "Valid XML". The biggest difference between them is that one fully complies with the XML specification, while the other has its own "Document Type Definition (DTD)".
 The process of comparing an XML document with its DTD file to see if it complies with DTD rules is called validation. This process is usually handled by a software called parser.
 A valid XML document must also start with an XML declaration, for example:
 
  Different from the above example, in standalone( Independent) attribute, the setting here is "no", because it must be used together with the corresponding DTD. The DTD file is defined as follows:
 PUBLIC "dtd-name">
 Where:
 "!DOCTYPE" means you want to define a DOCTYPE;
 "type-of-doc" is the name of the document type, which is defined by you, usually the same as the DTD file name;
 "SYSTEM/PUBLIC" Only one of the parameters is used. SYSTEM refers to the URL of the private DTD file used by the document, while PUBLIC refers to the URL of the public DTD file used by the document.
 "dtd-name" is the URL and name of the DTD file. All DTD files have the suffix ".dtd".
 We still use the above example, it should be written like this:
 
 gt ;

  2. DTD related terms

 What is DTD, we have briefly mentioned it above. DTD is an effective method to ensure that the XML document format is correct. You can compare the XML document and the DTD file to see whether the document conforms to the specification and whether the elements and tags are used correctly. A DTD document contains: the definition rules of elements, the definition rules of relationships between elements, the attributes that can be used by elements, and the rules of entities or symbols that can be used.
 The DTD file is also an ASCII text file with the suffix .dtd. For example: myfile.dtd.
 Why use DTD files? My understanding is that it meets network sharing and data interaction. The biggest benefit of using DTD is the sharing of DTD files. (This is the PUBLIC attribute in the DTD description statement above). For example, if two people in the same industry and different regions use the same DTD file as a document creation specification, their data can be easily exchanged and shared. If other people on the Internet want to add data, they only need to create a document according to the public DTD specification, and they can join immediately.
Currently, there are already a large number of written DTD files available. Targeting different industries and applications, these DTD files have established common element and label rules. You don't need to recreate them yourself, just add the new logos you need based on them.
 Of course, if you like, you can create your own DTD, which may match your document more perfectly. Creating your own DTD is also very simple. Generally, you only need to define 4-5 elements.
 There are two ways to call a DTD file:
 1. DTD directly included in the XML document
 You only need to insert some special instructions in the DOCTYPE declaration, like this:
 We have an XML document:
 
  
 
 We just insert the following code after the first line:
  
  
  
  
 ]>
  2. Call an independent DTD file
  Save the DTD document as a .dtd file, and then call it in the DOCTYPE declaration line. For example, save the following code as myfile. dtd
 
 
 
  Then call it in the XML document, in the first line After inserting:
  
  We can see that the calls to js in DTD documents and HTML are similar. Regarding how to write DTD documents, we will discuss the details of writing DTD documents and XML documents in the next chapter. The grammar is introduced together.
 Let’s learn about the terms related to DTD:
 1.Schema (planning)
 Schema is the description of data rules. Schema does two things:
 a. It defines the element data type and the relationship between elements;
 b. It defines the content type that the element can contain.
  DTD is a schema for XML documents.
 2.Document Tree (Document Tree)
 We have already mentioned "Document Tree" in Chapter 2. It is an image representation of the hierarchical structure of document elements. A document structure tree contains the root element, which is the top-level element (that is, the first element immediately following the XML declaration statement). Look at the example:
  
 
 author>
 

 
  The above example is arranged in a three-level structure into a "tree" shape, in which is the root element. In XML and DTD files, the first element defined is the root element.
 3.Parent Element/Child Element
  A parent element refers to an element that contains other elements, and the contained element is called its child element. Look at the "structure tree" above, where is the parent element, , <author> are its child elements, and <myfile> is the child element of <filelist>. The last-level elements that do not contain any child elements like <title> are also called "page elements". <br> 4.Parser (parsing software) <br> Parser is a tool software that checks whether XML documents comply with DTD specifications. <br>  XML parsers have developed into two types: one is the "non-confirmation parser", which only detects whether the document complies with XML syntax rules and whether the document tree is established with element identifiers. The other is the "confirmation class paeser", which not only detects the document syntax and structure tree, but also compares and analyzes whether the element identifiers you use comply with the specifications of the corresponding DTD file. <br>Parser can be used independently or as part of an editing software or browser. In the following list of related resources, I have listed some of the currently popular parsers. <br> Okay, through the study of Chapter 3, we have learned some basic terms of XML and DTD, but we still don’t know how to write these files and what kind of syntax needs to be followed. In the next chapter, we will focus on writing Syntax for XML and DTD documents. Please continue browsing, thank you!

Chapter 4 XML Syntax
Outline:
1. XML syntax rules
2. Element syntax
3. Comment syntax
4. CDATA syntax
5. Namespaces syntax
6. Entity syntax
7. DTD The syntax of

Through the study of the previous three chapters, we already have an understanding of what XML is, its implementation principles and related terminology. Next, we will start to learn the syntax specifications of XML and write our own XML documents.

 1. XML syntax rules

  XML documents are similar to the original code of HTML, and also use tags to identify content. The following important rules must be followed when creating XML documents:
  Rule 1: There must be an XML declaration statement
  We have already mentioned this when studying in the previous chapter. The declaration is the first sentence of the XML document, and its format is as follows:
 
 The function of the declaration is to tell the browser or other Handler: This document is an XML document. The version in the declaration statement indicates the version of the XML specification that the document complies with; standalone indicates whether the document comes with a DTD file, if so, the parameter is no; encoding indicates the language encoding used in the document, and the default is UTF-8.
  Rule 2: Whether there is a DTD file
  If the document is a "valid XML document" (see the previous chapter), then the document must have a corresponding DTD file and strictly comply with the specifications set by the DTD file. The declaration statement of the DTD file follows the XML declaration statement in the following format:
  PUBLIC "dtd-name">
 Among them:
 "!DOCTYPE" means that you want to define a DOCTYPE;
 "type-of-doc" is the name of the document type, which is defined by you. It is usually the same as the DTD file name;
 Only one of the two parameters "SYSTEM/PUBLIC" is used. SYSTEM refers to the URL of the private DTD file used by the document, while PUBLIC refers to the URL of the public DTD file used by the document.
 "dtd-name" is the URL and name of the DTD file. All DTD files have the suffix ".dtd".
 We still use the above example, it should be written like this:
 
 gt ;
  Rule 3: Pay attention to your capitalization
  In XML documents, there is a difference between upper and lower case.

and

are different identifiers. Note that when writing elements, the case of the front and rear identifiers must be the same. For example: ajie, it is wrong to write ajie.
You'd better develop a habit of either all capital letters, all lower case letters, or capitalize the first letter. This reduces documentation errors caused by case mismatches.
  Rule 4: Add quotes to attribute values ​​
In HTML code, attribute values ​​can be quoted or not. For example: word and word can both be interpreted correctly by the browser.
 But in XML, it is stipulated that all attribute values ​​must be quoted (can be single quotes or double quotes), otherwise it will be regarded as an error.
  Rule 5: All tags must have corresponding closing tags
In HTML, tags may not appear in pairs, such as
. In XML, it is stipulated that all tags must appear in pairs. If there is a start tag, there must be an end tag. Otherwise it will be considered an error.
 Rule 6: All empty tags must also be turned off
 An empty tag is a tag with no content between the pair of tags. For example,
, XML easy learning manual and other logos. In XML, it is stipulated that all tags must have an end tag. For such empty tags, the processing method in XML is to add / at the end of the original tag, and that's it. For example:
  
should be written as
;
  should be written as;
 XML easy learning manual should be written as XML easy learning manual

  2. Syntax of elements

 The element consists of a pair of logos and their content. Like this: ajie. The name of the element and the name of the identifier are the same. Identities can be further described using attributes.
 In XML, there are no reserved words, so you can use any word as an element name as you like. However, the following specifications must also be followed:
 1. The name can contain letters, numbers and other letters;
 2. The name cannot begin with numbers or "_" (underscore);
 3. The name cannot begin with the letters xml (or XML or ..)
4. The name cannot contain spaces
5. The name cannot contain ":" (colon)
In order to make the elements easier to read, understand and operate, we have some suggestions:
1. Do not use " in the name ." Because in many programming languages, "." is used as an attribute of an object, for example: font.color. For the same reason, it is best not to use "-". If it must be used, replace it with "_";
 2. Keep the name as short as possible.
 3. Try to use the same standard for capitalization and capitalization of names.
 4. The name can use non-English characters, such as Chinese. But some software may not support it. (IE5 currently supports Chinese elements.)
In addition, add a little explanation about attributes. In HTML, attributes can be used to define the display format of elements. For example: word will display word in red. In XML, attributes are just descriptions of identifiers and have nothing to do with the display of element content. For example, the same sentence: word will not display the word in red. (So, some netizens will ask: How to display text in red in XML? This requires the use of CSS or XSL, which we will describe in detail below.)

  3. Syntax of comments

  Comments are for ease of reading and understanding , additional information added to the XML document will not be interpreted by the program or displayed by the browser.
 The syntax of comments is as follows:
 
 As you can see, it is the same as the comment syntax in HTML, which is very easy. Developing good commenting habits will make your documents easier to maintain, share, and look more professional.

4. Syntax of CDATA

CDATA stands for character data, which is translated as character data. When we write XML documents, we sometimes need to display letters, numbers and other symbols themselves, such as "  
  For example:
  ajie

]]>
 The content displayed on the page will be "ajie"

 5. The syntax of Namespaces

 Namespaces is translated as namespace. What does the namespace do? When we use other people's or multiple DTD files in an XML document, there will be such a contradiction: because the identifiers in XML are created by ourselves, in different DTD files, the identifier names may be the same but have different meanings. This may cause data confusion.
 For example, in one document

sample
wood table
, represents a table,
  And in another document,
namelist
, represents a table. If I need to work on both documents at the same time, a name conflict will occur.
 To solve this problem, we introduced the concept of namespaces. Namespaces distinguish these identifiers with the same name by adding a URL to the identifier name.
 Namespaces also need to be declared at the beginning of the XML document. The declaration syntax is as follows:
 
 where yourname is the name of the namespaces defined by you, and URL is the URL of the namespace.
  Assuming that the "table
" document above comes from http://www.zhuozi.com, we can declare it as
 
Then use the defined namespace in the following logo:
 
wood table

 This will distinguish the two . Note: Setting the URL does not mean that this logo really needs to be read from that URL, it is just a sign of distinction.

 6. Entity syntax

 Entity is translated as "entity". Its function is similar to the "macro" in word, and can also be understood as a template in DW. You can pre-define an entity and then call it multiple times in one document, or call the same entity in multiple documents.
  Entities can contain characters, text, etc. The benefits of using entities are: 1. It can reduce errors. Multiple identical parts in the document only need to be entered once. 2. It improves maintenance efficiency. For example, if you have 40 documents that all contain copyright entities, if you need to modify this copyright, you don't need to modify all the files. You only need to change the originally defined entity statement.
 XML defines two types of entities. One is the ordinary entity we are talking about here, used in XML documents; the other is parameter entity, used in DTD files.
  The definition syntax of entity is:
  
    ]
  >
 For example, I want to define a piece of copyright information:
  
  < ;!ENTITY copyright "Copyright 2001, Ajie. All rights reserved"
  ]
  >
 If my copyright information content is shared with others in an XML file, I can also use the external call method. The syntax is like this:
  
  
  ]
  >
  The reference syntax of a defined entity in the document is: &entity-name;
  For example , the copyright information defined above is written when calling: ©right;
 The complete example is as follows, you can copy it and save it as copyright.xml to view the example:
 

  
 ]>
  
  XML
 ajie
 ajie@aolhoo.com
  20010115
 DTD is "valid XML" "Document" is a necessary file. We use DTD files to define the rules and mutual relationships of elements and identifiers in the document. How to create a DTD file? Let us learn together:
 1. Set elements

  Elements are the basic components of XML documents. You need to define an element in the DTD and then use it in the XML document. The definition syntax of an element is:

 Note:

 " "DESCRIPTION" after the declaration ", is the name of the element;
 "(#PCDATA, DEFINITION)*>" is the usage rule of the element. Rules define what elements can contain and how they relate to each other. The following table outlines the rules of elements:
2. Element rule table:
Symbol Meaning Example
#PCDATA Contains character or text data
Element MYFILE contains a text data
#PCDATA, element -name contains text and other sub-elements
MYFILE element must contain text and TITLE sub-elements
, use comma separated sorting
MYFILE element must contain the three sub-elements TITILE,AUTHOR,EMAIL in order
| Use "|" to indicate or
The MYFILE element must contain TITLE, or the AUTHOR or EMAIL sub-element
name can only be used once
The MYFILE element must contain the TITLE sub-element and can only be used once.
name? Use once or not use
The MYFILE element must contain the TITLE sub-element and can only be used once; it can contain or not contain the AUTHOR and EMAIL sub-elements, but If used, only once.
name+ Used at least once or multiple times
The MYFILE element must contain the TITLE sub-element and be used at least once; it can be followed by the AUTHOR sub-element, It does not need to be followed; the EMAIL sub-element must be included at the end and can only be used once.
name* Use once, multiple times, or not at all
The MYFILE element can contain one, multiple or no TITLE sub-elements
( ) Set groups, which can be nested


The element MYFILE contains one or more text or TITLE sub-elements.


The MYFILE element must contain some content, which may be a comment; or it may be multiple groups, and the group may contain: one, multiple Either no TITLE sub-element, followed by one or no AUTHOR sub-element, followed by a required EMAIL sub-element.

In addition, we can also define attributes for elements. Since we do not recommend using attributes, we will not go into details here.
 Finally, let’s summarize some of the content learned in the first four chapters and write a simple example including DTD, XML, and Script to facilitate readers’ understanding:
 1. Save the following file as myfile.dtd
 
 
 
  2. Then create an XML document myfile.xml:
 
 
 
 ;
 

  3. Create an HTML document myfile.html
 
 


 
 Calling XML data in HTML
 
 
 Title:
 

 Author:
 

 
 
  4. Open myfile.html with IE5.0 or above browser You can see the effect.
 Okay, by the end of Chapter 4 today, we already have some basic understanding of XML. In Chapter 5, I will explain to you a successful example of practical application of XML to demonstrate the powerful functions of XML. Let's take a look at the next chapter: XML example analysis.
Chapter 5: XML instance analysis

 Outline:
 One: Example effect
 Two: Example analysis
 1. Define a new logo.
 2. Create XML document.
 3. Create the corresponding HTML file.
 XML is widely used in different fields, such as MathML in the technology field, WML in wireless communication applications, SVG in network images, etc. Here we focus on the application of XML on the web. The application of XML on the web mainly takes advantage of its powerful data manipulation capabilities. Generally using XML with server-side programs such as JavaScript and ASP, almost all application needs on the network can be realized.
 Considering the convenience of explanation, we introduce a simple example below, which does not include server-side programs. The purpose is to give you a perceptual understanding of XML's data manipulation capabilities.
 Okay, let’s first [click here] to see the effect of the example. (Please use IE5.0 or above to open it)
This is a simple CD record data retrieval function. You can see the relevant information of a single CD by clicking "Previous" and "Next". We originally used two methods to achieve this effect:
 1. Use DHTML to hide the data in different layers and display it sequentially through mouse events;
 2. Use background programs (such as ASP, CGI, PHP, JSP, etc.) , calling server-side data.
  But in this example, when we open the original code of the page, we can see that there is no DHTML DIV or form action. It is completely implemented in XML. Let’s analyze its production process below:

The first step: Define the new logo.

Based on the actual CD data, first create a new identifier named ; secondly, establish its related data identifiers, which are: CD name , singer <artist>, publication year <year> , Country <country>, Issuing Company <company> and Price <price>; Finally, a logo named Catalog <catalog> must be created. Why create another <catalog> logo? Because it is stipulated in the XML document that there must and can only be one root element (identity), we have multiple CD data, and these data are in a parallel relationship, so we need to create a root element for these parallel elements. <br> The definitions and relationships of the above elements are fully compliant with XML standards and do not require a special DTD file to be defined, so the DTD definition can be omitted. If we want to use DTD to define, the above process can be expressed as: <br>  <br>  <br>  <br>  <br>  <br> Company (#PCDATA)><br> <br> This code means: the element CATALOG contains multiple CD sub-elements, and the sub-element CD in turn contains Title, Artist, Year, Country, Company , Price has six sub-elements, and their contents are all defined as text (characters, numbers, text). (Note: For specific syntax instructions, please see the introduction to DTD in the previous chapter)<br></catalog></catalog></price></company></country></year></artist>

 Step 2: Create an XML document.

 

 
 
 
  Columbia
  10.90
  1985
  

 
 < ;TITLE>Hide your heart
  Bonnie Tylor
 UK
  CBS Records
   9.90
 1988
 

 
 t;
 
  RCA
  9.90
  1982
  

  
 < ;TITLE>Still got the blues
  Gary More
 UK
 Virgin redords
  10.20< ;/PRICE>
 1990
 
 
 t;
 
  BMG
  9.90
  1997
  

  lt;/CATALOG>
The above code first uses the declaration statement to indicate that this is an XML document, and its format complies with the XML 1.0 standard specification. Then there is the document content, and the structure tree is very clear:
  
  
 ……
  

  
      
> ;
 

  A total of 5 sets of data are defined. We save the above code as a cd.xml file for call.

 Step 3: Create the corresponding HTML file.

 1. Import XML data.
 We know that among the currently popular browsers, only Microsoft's IE5.0 or above browser currently supports XML. IE supports inserting XML through the object object in HTML, and imports data through the XMLDocument.load() method of js. Let’s look at the code:
   CLASSID="clsid:550dda30-0541-11d2-9ca9-0060b0ec3d39" ID="xmldso">
  
  Define a object, whose ID name is xmldso. Then use js to introduce xml data in the head area:
 


  3. Action operation.
 Finally, provide a browse button for the data:
 
 
 And use js to complete two mouse click functions: movenext() and moveprevious(). Add the following code to the head area:
 
 
 


 
  
  


  
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn