Xml
Document Format FOR XML
First, the basic unit of the content of the XML document-the element, its syntax format is as follows:
"Label" Text content 〈/label
Elements are made up of start tags, element content, and end tags. The user places the data object to be described between the start tag and the end tag. For example:
< name > Wang Ping </name >
Regardless of how long or complex the text content is, there are other elements in the XML element that can be nested so that the relevant information forms a hierarchical structure. In the following example, the elements of <employees> include all staff information, each employee is described by the <employee> element, and the <employee> elements are nested in <name> and < salary> element.
Example 1:
<employees>
<employee>
<name>lars peterson</name>
<salary>25000</salary>
</employee>
<employee>
<name>charlotte M. cooper</name>
<salary>34500</salary>
</employee>
</employees>
In addition to elements, the valid objects that can appear in an XML document are processing instructions, annotations, root elements, child elements, and attributes.
Processing instructions
The processing instruction provides the XML parser with information so that it can correctly interpret the contents of the document, and its initial identification is "?>", and the end tag is "". A common XML declaration is a processing instruction:
<?xml version= "1.0"?>
Processing instructions can also be used for other purposes, such as defining whether a document is encoded in GB or Unicode encoding, or applying a stylesheet file to an XML document for display.
Comments
Comments are character data that is used as an interpretation in an XML file, and the XML processor does not do anything with them. Annotations are caused by "<!--" and "-->" and can appear anywhere between XML elements, but they cannot be nested:
<!--This is a note-->
root element and child element
If an element starts from the preamble of the header to the end of the file and contains all the data information in the file, we call it the root element.
XML elements can be nested, so elements that are nested are called child elements. In the previous example,<employee> is the child element of <employees>.
Property
Property provides further descriptive information to the element, which must appear in the start tag. Properties appear as name/value pairs, property names cannot be duplicated, names are separated from values by the equals sign "=", and values are enclosed in quotation marks. For example:
<salary currency= "US $" > 25000 </salary>
The properties in the example above indicate that the monetary unit of the salary is in dollars.
Syntax FOR XML
The basic structure of an XML document consists of a preamble and a root element. The preamble includes XML declarations and DTDs (or XmlSchema), DTDs (document type Define, file-defined types), and XmlSchema are all used to describe the structure of an XML document, which describes how elements and attributes are linked together.
For example, a complete XML document is formed in front of the document in Example 1, preceded by the following preambular section:
<?xml version= "1.0"?>
<! DOCTYPE employees SYSTEM "EMPLOYEES.DTD" >
There is only one root element in an XML document, all other elements are its child elements, and,<employees> is the root element in example 1.
An XML document should first be "well-formed" (well-formed), the formal definition of which is located at:
Http://www.w3.org/TR/REC-xml
In addition to satisfying the unique attributes of the root element, a well-formed XML document includes:
The start and end tags should match: The end tag is essential;
Case should be consistent: XML is sensitive to the case of letters,<employee> and <Employee> are completely different two tags, so the end tag in the match must pay attention to the same case;
Elements should be nested correctly: child elements should be fully included in the parent element, and the following example is a nested error:
<A>
<B>
</A>
</B>
The correct nesting method is as follows:
<A>
<B>
</B>
</A>
Attributes must be included in quotation marks;
The attributes in the element are not allowed to be duplicated.
The "validity" of an XML document means that an XML document should comply with a DTD file or schema, and that "valid" XML documents must be "well-formed", and we will elaborate on them later.
Namespaces FOR XML
XML documents are likely to define many elements or attributes with the same name and different meanings, especially when the different XML documents are merged into each other, creating conflicts more easily. Namespaces are proposed to solve this problem. It distinguishes it with a URI (Uniformresource indicator, Uniform resource indicator), a collection of all names that appear in the elements and attributes of an XML file. The following example:
<pr:payment xmlns:pr= "http://www. Microsoft.com/payroll ">
<pr:employee>lars peterson</pr:employee>
<pr:description>reimburse expenses</pr:description>
<pr:total>199.76</pr:total>
</pr:payment>
With a namespace, the user can guarantee that the name used in the file is unique. The definition of an element's property xmlns means that a namespace is specified for the element. The namespace_name must be a valid URI.
If Local_prefix (local prefix) is omitted, then the default namespace is formed:
<payment xmlns= "Http://www.microsoft.com/acct" >
<customer>1234</customer>
<amount>500.00</amount>
<date_received>12-03-2000</date_received>
</payment>
If a default namespace is defined for an element, then the element and its child elements, including their properties, are automatically part of the namespace, not to be marked in front of each element and attribute one by one.
At the beginning of this article, we made an overview of XML as a new technology, analyzed its advantages and disadvantages, and prospected its good application prospects. In the latter part of this paper, the related syntax and format of XML document are briefly described, and several useful development tools are introduced. In the future, we will be fully developed, in-depth into the XML technology, to explore this wonderful world!