I recently read a book titled basic XML tutorials-getting started, DOM, Ajax, and Flash (Beginning XML with DOM and Ajax), which gives me a deeper understanding of XML, it is mainly about the application of XML on the webpage. In the past, XML was mainly used to configure data and databases, and DOM and SAX technologies were also used to read and write data. However, XML technology was rarely used on webpages. So here we will mainly introduce the XML Web application I learned, includes basic XML knowledge, DTD document type definition, CSS Cascading Style Sheet, XSLT extensible style sheet language conversion, and a few JavaScript operations.
XML is widely used today because of its powerful data description and scalability. The standard XHTML language is an application of XML. In addition, XML is used to express mathematical formats such as MathML, vector graph SVG, RSS, and even MSN Chat records.
The main points of XML are as follows:
- XML is not a language. Its rules are used to construct other languages;
- XML creates a tag-based language for marking content;
- XML is based on SGML (standard General Markup Language) and is compatible with SGML
XML is applicable if the following conditions are met:
- This document contains one or more elements
- This document only contains one document element, used to include other elements
- Each element ends correctly.
- Case Sensitive Element
- Attribute values are enclosed in quotation marks and not empty.
Therefore, HTML does not meet the requirements because its tags are case-insensitive and the order of tags is not required. attributes do not need to be enclosed by quotation marks. For example
Code
<P> <Strong> Paragraph text </p> </STRONG>
<Td nowrap> A table cell </td>
Is not affected, but is not allowed in the XML format. Modify the following to conform to the XML standard. Code
<P> <strong> Paragraph text </strong> </p>
<Td nowrap = "true"> A table cell </td>
Tag refers to the content of the description, that is, the content contained in angle brackets. An element refers to a start tag to an end tag and its content.
The following is an example of a standard XML document.
Code
<? Xml version = "1.0" encoding = "UTF-8"?>
<! -- This XML document describes a DVD library -->
<Library>
<DVD id = "1">
<Title> Breakfast at Tiffany's </title>
<Format> Movie </format>
<Genre> Classic </genre>
</DVD>
<DVD id = "2">
<Title> Contact </title>
<Format> Movie </format>
<Genre> Science fiction </genre>
</DVD>
<DVD id = "3">
<Title> Little Britain </title>
<Format> TV Series </format>
<Genre> Comedy </genre>
</DVD>
</Library>
The first line is an XML declaration. Optional. At that time, it was generally convenient for the program to be processed. If you need to be in the first line of the document, and there is no content or space in front of it. The version attribute refers to the version number. Encoding is a character set, if not specified, the default is UTF-8, use Chinese words, need to declare as GBK or GB2312. In addition, the standalone attribute has a value of yes or no, indicating whether an external file is required. The three attributes are optional, but the order must follow version, encoding, and standalone.
The second line is a comment, with "<! -- "Start," --> "end, consistent with HTML.
The third line is the beginning of the document element (root element), where all elements are contained. The XML format has only one root element. Elements can contain child elements, text, empty elements, and hybrid elements. Empty elements refer to the following two methods of writing with only labels and no text. In some parsing programs, the two methods are different.
Code
<ElementName> </elementName>
<ElementName/>
A hybrid element contains both text and child elements.
The XML Naming rules are as follows:
- It cannot start with a number or punctuation.
- Cannot contain spaces
- If it is not a namespace, do not use a colon
- Case Sensitive
Processing instruction (PI) refers to transmitting Processing information to other applications. XML is not processed, but directly transmitted to the application. For example, the application of the specified XSL style sheet:
Code
<? Xml-stylesheet type = "text/xsl" herf = "stylesheet. xsl"?>
There are two main XML Processing Methods: tree-based DOM (Document Object Model) and event-based SAX (Simple Application Programming Interface) parser.
For DOM, the entire XML file is read, parsed into a tree structure, and then the values and modifications of each element can be queried. For SAX, It is the reading element of the sentence, and then throwing away the sentence. Simply put, it calls back a function to tell the user what the tag is currently read and then processes the following content. The former is easy to read at a time, and then perform multiple queries and modifications, but it consumes memory. The latter will not be saved and processed by the user after being read. If the XML file is large and you only need to process a few elements in the header, you should select the SAX method. When you get the information you need, you will not process it any more, you do not need to parse the entire document.