Home > Backend Development > XML/RSS Tutorial > An introductory tutorial on the basic concepts and syntax of XML markup language (picture)

An introductory tutorial on the basic concepts and syntax of XML markup language (picture)

黄舟
Release: 2017-03-10 20:04:26
Original
1829 people have browsed it

This article mainly introduces the basic concepts and syntax introductory tutorial of XML markup language. XML is also widely used to mark data as XML data exchange format. Friends in need can refer to it


XML stands for Extensible Markup Language. It is a text-based markup language derived from the Standard Generalized Markup Language (SGML).

XML tags identify data and are used to store and organize data, rather than specifying how to display it, like HTML for displaying data. XML won't replace HTML in the near future, but it introduces possibilities by adopting many of the successful features found in HTML.

Here are three important characteristics of XML that make it useful for a wide variety of systems and solutions:

XML is extensible: XML allows us to create our own suitable applications A self-describing tag or language for a program.
XML carries data, but does not render it: XML allows us to store data regardless of how it will be rendered.
XML is a public standard: XML was developed by an organization called the World Wide Web Consortium (W3C), and it is available as an open standard.


Purposes of XML
This short list of uses for XML says it all:

XML can work behind the scenes to simplify the creation of HTML documents for large websites .
XML can be used to exchange information between organizations and systems.
XML can be used to unload and reload the database.
XML can be used to store and organize data, and can also customize data processing requirements.
XML It's easy to incorporate stylesheets to create almost any desired output.
In fact, any type of data can be represented as an XML document.


What are tags?
XML is a markup language that defines a set of rules for encoding documents in a human-readable and machine-readable format. So what exactly is a markup language? Markup is information added to a document that improves its meaning in some way. It identifies parts and how they relate to each other. More specifically, a markup language is a set of symbols that can be placed into the text of a document to divide and mark certain parts of the document.

The following example shows what an XML tag embedded in a block of text looks like:

<message>
    <text>Hello, world!</text>
</message>
Copy after login

This fragment contains markup symbols, or tags, such as and ... . The tags and represent the beginning and end of this XML snippet. The tags and wrap the text Hello world!.

Is XML a programming language?
A programming language used to create computer programs consists of syntax rules and its own vocabulary. These programs instruct the computer to perform specific tasks. XML does not qualify as a programming language because it does not perform any calculations or algorithms. It is usually stored in a simple text file and processed by special software capable of interpreting XML.

XML syntax
The following is a complete XML document:

<?xml version="1.0"?>
<concat-info>
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
</concat-info>
Copy after login

You can notice that there are two types in the above example Information:

tag, such as .
Text or character data, such as _Tutorials Point and (011) 123-4567_.
The following figure describes the syntax rules for writing different types of tags and text in XML documents:
An introductory tutorial on the basic concepts and syntax of XML markup language (picture)

Let's take a closer look at each component in the above figure:

XML Declaration
XML documents can have an optional XML declaration. It can be written in the following form:

<?xml version="1.0" encoding="UTF-8"?>
Copy after login


Here version is the XML version, and encoding specifies the character encoding used in the document.

Syntax rules for XML declaration

XML declaration is case-sensitive and must start with "", where "xml" is lowercase .
If the document contains an XML declaration, it must be the first statement in the XML document.
The XML declaration must be the first statement of the XML document.
You can use an HTTP protocol to override the encoding value specified in the XML declaration.


Tags and elements
The structure of an XML file consists of several XML elements, also called XML nodes or XML tags. The name of an XML element is enclosed in angle brackets < > as follows:

<element>
Copy after login

Syntax rules for tags and elements

Element syntax: Each XML element must be closed or With start and end elements, like this:

<element>....
Copy after login

or abbreviated form, like this:

<element/>
Copy after login

Element nesting: An XML element can contain multiple XML elements as Its child elements, but child elements cannot overlap. For example, an element's closing tag must have the same name as the most recently matched opening tag.

The following example shows incorrect nested tags:

<?xml version="1.0"?>
<contact-info>
<company>TutorialsPoint   
<contact-info>
</company>
Copy after login

The following example shows correct nested tags:

<?xml version="1.0"?>
<contact-info>
<company>TutorialsPoint</company>
<contact-info>
Copy after login

Root element: An XML document has only one root element. For example, the following is an incorrect XML document because the x and y elements appear at the top level without a root element:

<x>...</x>
<y>...</y>
Copy after login

下面的例子展示了正确形式的 XML 文档:

<root>
    <x>...</x>
    <y>...</y>
</root>
Copy after login

区分大小写: XML 元素的名称区分大小写。这意味着元素的起始和结束标签大小写必须一样。

例如 和 __ 就不同。

属性
attribute 使用名/值对给元素指定一个属性(property)。一个 XML 元素可以有一个或多个属性(attributes)。例如:

<a href="http://www.php.cn/">Tutorialspoint!</a>
Copy after login

这里 href 就是属性名称,而 http://www.php.cn/ 就是属性值。

XML 属性的语法规则

XML 属性名区分大小写(和 HTML 不一样)。也就是说 HREF 和 href 会被认为是两个不同的 XML 属性。
在语法中相同的属性不能有两个。下面的例子展示了不正确的语法,因为属性 b 被指定了两次:

<a b="x" c="y" b="z">....</a>
Copy after login

属性名定义不带引号标记,而属性值必须显示在引号标记中。下面的例子演示了不正确的 XML 语法:

<a b=x>....</a>
Copy after login

在上面的语法中,属性值没有定义在引号标记中。

XML 引用
引用 通常允许我们在 XML 文档中添加或包含附加的文本。引用始终以符号 "&" 开始,这是一个保留字符,以符号 ";" 结尾。XML 中有两种类型的引用:

实体引用: 一个实体引用的起始和结束定界符之间包含一个名称。比如 &,其中 amp 就是名称。这个 name 通常指向一个预定义的文本字符串或标记。

字符引用: 这些包含引用比如 A 包含一个 hash 标记("#"),后面紧跟一个数字。这个数字始终指向一个字符的 Unicode 码。在这里,65 指向字母 "A"。

XML 文本
XML 元素和 XML 属性的名称区分大小写。这意味着元素的开始和结束标签大小写必须一致。
为了避免字符编码的问题,所有的 XML 文件都应该保存为 Unicode UTF-8 或者 UTF-16 文件。
空白字符,比如空格,制表符以及 XML 元素和 XML 属性之间换行符会被忽略。
有些字符是 XML 语法本身保留的。因此,不能直接使用它们。要使用它们,就要使用一些替代实体。下面列出了一些:
An introductory tutorial on the basic concepts and syntax of XML markup language (picture)


The above is the detailed content of An introductory tutorial on the basic concepts and syntax of XML markup language (picture). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template