A basic XML syntax

Source: Internet
Author: User

I. XML BASIC syntax

1.1 XML Introduction

1) XML is Extensible Markup Language (extensible Markup Language).

2) XML is a software and hardware-independent information transfer tool.

3) XML is in the form of text in a text file, usually the suffix of the file is ". xml", for example: User.xml.

4) XML is designed to transmit information (especially complex data) rather than display data.

5) XML can depict data in a tree-like structure. Because of this feature, in addition to transmitting data, we use XML as a configuration file.

6) XML is a markup language, much like HTML.

7) XML tags are not predefined and need to be self-defined.

8) XML is designed to be self-descriptive.

9) XML is the standard for the World Wide Web Consortium, World Wide Consortium, a 1994-based organization whose purpose is to ensure its versatility by facilitating the development of generic protocols to inspire the full potential of the web.

XML comment: <! comment Content--

1.2 XML elements

1) An XML document contains XML elements.

2) The XML element refers to the part from the start tag (inclusive) to the end tag (inclusive).

3) elements can contain other elements (tag nesting use), text, or a mixture of both.

4) elements can also have attributes.

For example, the contents of the XML file are labeled,<tag></tag> tags are paired.

1.3xml Properties

1) XML elements can include attributes in the start tag (that is, attributes are defined in the previous tag), properties (Attribute) are not typically used to hold data, but are used to set, describe some characteristics of a tag, and provide additional (additional) information about the element Information. Properties typically provide information that is not part of the data, but is important for applications that need to handle this element.

Precautions:

The property must be in the form of a property name = property value.

Elements can contain elements, but they cannot be cross-used! A nested relationship must be complete.

2) attribute values for XML attributes must be in quotation marks, single quotes, or double quotes. If the property value itself contains double quotation marks, it is necessary to enclose it in single quotation marks, or you can use an entity reference.

For example: <oracle user= "Wang's ' Connection '" id= "SSS" >

3) After the label signature, you can define several attributes, each of which should be separated by a space.

1.4 Entity References

To solve the special characters of XML used in attribute values, we can use similar escape characters to describe them.

Character
Escape character (entity reference)

< &lt;

> &gt;

& &amp;

' &apos;

"&quot;

Precautions:

You cannot have special characters in text

In fact, in the attribute values & and < are special characters that are not valid (but > are legal) and must be escaped. The rest can be used, but a better habit is to use escape expressions when encountering characters with special meanings in XML.

1.5 CDATA Segment

In some cases, we want to use a lot of XML-sensitive characters in XML, and we don't want to escape them one at a time. Using CDATA segments is ideal.

1) syntax format: <! [Cdatat [Ignore checked text]] >

2) Writing the contents of the text in CDATA, the text content is ignored and checked, regardless of whether it contains XML-sensitive content, and is treated as plain text.

For example:

<content>

<! [cdata[

<script language= "JavaScript" >

function SayHello () {

Alert ("Hello");

}

]]>

</content>

3) The exchange of data in the industry requires the same XML file format, so you need to adhere to the canonical XML file format, for example, two XML files to have the same element nesting relationship, the same attribute definition, the same element order, the same number of occurrences of elements and so on.

As follows: Two copies of the same data, but different structure of the XML file, cannot exchange data.

A school's XML file:

< computer books >

< title isbn= "1234" >xml's past life </title >

< price >50</price >

< introduction > A book </Introduction to XML >

< author > Liyi </author >

</Computer Books >


B School's XML file:

<computer_book>

<isbn>1234</isbn>

<bookname author= "Liyi" >xml's Past life </bookname>

<price>50</price>

<brief> a book about XML </brief>

</computer_book>

1.6 DTD Declaration Element
1) in a DTD, elements are declared by element declarations (for declaring and constraining elements).

Note: XML allows only one root tag (root node).

2) DTD declaration element syntax:

<! element name (element content) >

<! Element element name elements Category >

Note: If you use a DTD to declare an element (or use DTD validation), you can use only the declared element, including the child element under the element, in XML. You cannot use elements that are not in the DTD declaration.

1.7 DTD declaration element: declares an empty element.

An empty label is declared, and the label contains no content.

Example: <! ELEMENT page empty>

Corresponds to:<page/> or <page></page>

Note: Shorthand:<page/> usually in XML if there is no post-tag to do so, the HTML is heavily used. If there is no content in the label.

1.8 DTD declaration element: contains Pcdata

Define the contents of the element as text content (write whatever you Can)

1) Syntax: <! Element name (#PCDATA) >

Example: <! ELEMENT page (#PCDATA) >

Corresponds to:<page>wang</page> or <page></page>

Note: Pcdata is a text that will be parsed by the parser, and the text will be checked by the parser entity and tagged.

2) Only text content can appear in the page tag at this time, but be aware that the text content needs to be checked, that is, the sensitive characters of the XML cannot appear.

<page>7<0</page> it doesn't work.

<page><! [CDATA [7<0]]></page> this is OK.]

1.9DTD declaration element: An element with child elements. Child elements (list of child elements)

That is, the label appears in the constraint tag, or there are child elements in the constraint element.

1) Syntax: <! element name (child element 1, child element 2,...) >

2) When declaring the child elements contained in the current element, we declare the child elements below.

Example: <! ELEMENT jdbc (oracle,mysql) >
<! ELEMENT Oracle (#PCDATA) >
<! ELEMENT MySQL (#PCDATA) >
The above declaration indicates that there is a label called JDBC, which can only contain two tags, respectively, Oracle and MySQL, and these two tags also declare that their content can be arbitrary text.
Corresponding:
<jdbc>
<oracle>wang</oracle>
<mysql>shengtao</mysql>
</jdbc>
Note: The order in which Oracle and MySQL appear in the JDBC tag must be consistent with the order of the elements that are declared by JDBC.
Error One:
<jdbc>
<mysql>shengtao</mysql>
<oracle>wang</oracle>
</jdbc>
Error Two:
<jdbc>
<oracle>wang</oracle>
<mysql>sheng</mysql>
<mysql>tao</mysql>--This is not possible, only once.
</jdbc>

1.10 DTD declaration element: Declares an element that appears only once
1) Syntax: <! element name (child element name) >
Example: <! ELEMENT page (RPEV) >
Correspondence: <page>
<prev>3</prev>
</page>
2) The Prev child element must appear once, and must appear only once in the "page" element.
1.11 DTD declaration element: Declares an element that can occur more than once.
For the number of occurrences of a child element, the DTD is also by specification, does not support a specific number, but supports quantifiers.
1) quantifiers that can appear on child elements:
? 0-1
* 0-Multiple times
+ 1-Multiple times
Example: <! ELEMENT jdbc (oracle,mysql+) >
Precautions:
With the above quantifiers, the order in which the child elements appear is not required to be consistent.
Without a usage word, the child element must appear and once, and the order of occurrence must match the order of the declared elements.
1.12 DTD declaration element: Child elements can only be when one of the cases
Example: <! ELEMENT sex (Man|woman) >
<! ELEMENT Man (#PCDATA) >
<! ELEMENT Woman (#PCDATA >
Corresponding:
<sex>
<woman>nv</woman><!--Label Two----
</sex>
Note: With is the direct use "," such as <! ELEMENT sex (Man,woman) >
1.13 DTD declaration elements: Child elements can be elements or text when they are available
Define a label a child element can be an element, or it can be text.
Example: <! ELEMENT Sex any>
1.14 DTD declaration elements: summary
Type name Type description
CDATA values are character data
(en1|en2| ...) Value is a value in the enumeration list
ID value is a unique ID value
IDREF value is the ID of another element
IDREFS value is a list of other element IDs
NmToken value is a valid XML name
Nmtokens value is a list of legitimate outgoing XML names
Entity value is a solid
Entities value is a list of entities
Notation value is the name of the symbol
The XML value is a pre-defined XML value
1.15 Attributes of elements declared in a DTD
1) in DTDs, attributes are declared by attlist
2) syntax for declaring attributes: <! Attlist element Name Property Name property type default Value >
Example: <! attlist Oracle User CDATA "User" >
<oracle></oracle><!--Default Value--
<oracle user= "user" ></oracle><!--default value does not work--
Note: Simply define the contents of the element as text content with #pcdata, other places with CDATA
Example: <! ELEMENT Package (#PCDATA) >
<! ELEMENT Package Color CDATA "FFFFFF" >
1.16 Property Types
1) CDATA: Text type
2) ID: The value of the property is unique in the XML (the property value is not repeatable)
3) enumeration (en1|en2| ...): The value of the property can only be one of the enumerated.
1.17 Constraints on attribute values
1) #REQUIRED: This property must be present in the tag (i.e. to be written), then the default value is not valid at this time.
Example: <! attlist Oracle User CDATA "user" #REQUIRED >
2) #FIXED: The value in the attribute is a fixed value (the value is given), or the value is one of its enumeration.
Example: <! attlist Oracle User CDATA "user" #FIXED ("User" | " Admin ") >
3) Default value "XXX": If the value is given directly at the default value, then it is the default value.
Example: <! Attlist Oracle ID CDATA "SSS" >
1.18 DTD Namespace Introduction
1) namespace (NameSpace), XML files allow custom tags, so there may be tokens from different source DTDs or schema files with the same name, in order to differentiate these tokens,
You will need to use the namespace.
2) The purpose of the namespace is to effectively distinguish between the same tokens from different DTDs, such as the following XML file that uses the namespace partition "table" and "table".
such as:<line><column> This is a table </column></line>
<product:table>
<type>coffee table</type>
<meterial>wood</meterial>
</product:table>

A basic XML syntax

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.