Explanation of basic knowledge of XML


insert image description here

1. Introduction to xml

XML (eXtensible Markup Language) is a markup language used to describe data.

It stores data in plain text and uses tags to mark the structure and meaning of the data. XML is designed to transmit and store data, but also as a format for configuration files and data exchange .

2. xml quick start

To create an xml document, there must be a document description and must be placed on the first line

<?xml version="1.0" encoding="UTF-8" ?>
  • version (required): Indicates the version of xml (1.0 and 1.1)

    • Note: 1.1 will not be backward compatible, generally 1.0
  • encoding (optional): Indicates the encoding format of xml, for example: UTF8, GBK, etc.

xml usage example:

<?xml version="1.0" encoding="UTF-8" ?>
<root>
    <person>111</person>
</root>

For xml documents, you can use a browser to open

insert image description here

3. XML element (tag) definition

  1. There can only be one root tag in an xml document

  2. User-definable labels, such as:<person></person>

    • There is one more end tag than the start tag/
  3. The definition of a label has a beginning and must have an end

  4. Tags must be reasonably nested

4. The naming convention of xml tags

  1. Labels are case sensitive
  2. Tags cannot start with a number or underscore
  3. Cannot start with keywords such as xml
  4. cannot contain spaces
  5. name can not have:

Note: xml will treat spaces and newlines as content when parsing

5. xml attribute definition and annotation

Property definition:

  • Each tag can have multiple attributes, but the attributes cannot be the same
  • Use quotes (single quotes or double quotes) between attributes and attributes
  • The naming convention of attribute names is the same as that of element names

Example:
insert image description here

Notes:

<!-- 注释内容   -->

Comments cannot be nested

6. Escape characters

In XML, several special characters are considered as reserved characters and cannot be used directly in the text, but need to be represented by escape characters.

The following are common escape characters in XML:

  • &lt;- means less than sign<
  • &gt;- means greater than sign>
  • &amp;- Representation and symbols&
  • &quot;- indicates a double quote"
  • &apos;- represents a single quote'

Example:
insert image description here

7. CDATA area

The CDATA (Character Data) area is a special area in XML for containing text data, which can contain any characters, including XML reserved characters and special characters, without escaping.

The CDATA area starts from <![CDATA[and ]]>ends with.

    <![CDATA[

    ]]>

Content in the CDATA section will be treated as plain text and will not be parsed as XML markup. CDATA sections are often used to contain data containing special characters or large amounts of text to avoid unnecessary escaping.

Example:

insert image description here

8. xml processing instructions

In XML, a processing instruction is an instruction used to instruct an XML parser or other processing tools to process an XML document. They <?start with , ?>end with , and are located at the beginning of the XML document.

Common XML processing instructions include:

  1. XML declaration: <?xml version="1.0" encoding="UTF-8"?>. This is the first processing instruction for an XML document and specifies the XML version and character encoding.
  2. Style sheet processing directives: <?xml-stylesheet type="text/xsl" href="style.xsl"?>. Used to reference XSL style sheets for transformation and rendering of XML documents.
  3. Entity processing instruction: <!ENTITY name "value">. Used to define entities that can be referenced and expanded into corresponding values ​​in XML documents.
  4. Namespace processing directives: xmlns:prefix="namespace". Used to define namespaces and namespace prefixes to identify namespaces of elements and attributes in XML documents.

These processing instructions can provide additional information about the XML document to the XML parser, or indicate how to process the XML document. Processing instructions usually appear at the beginning of an XML document, but are not required.

9. xml constraints

The constraints of xml refer to which elements can appear in xml and which elements cannot appear

Two common constraints are:

  1. DTD
  2. schema

DTD is a basic XML constraint language used to define the elements, attributes and entities of XML documents, as well as the relationships and constraints between them. DTDs use a set of rules to validate the structure and content of XML documents to ensure that they conform to the specifications defined in the DTD.

Schema is a more powerful and flexible XML constraint language for defining the structure, data types, and constraints of XML documents. XML Schema uses XML syntax to define elements, attributes, data types, constraints and relationships, and can perform more complex validation and constraints.

insert image description here

Guess you like

Origin blog.csdn.net/m0_63463510/article/details/132124619