XML introduction - XML: Extensible Markup Language 编辑

XML (Extensible Markup Language) is a markup language similar to HTML, but without predefined tags to use. Instead, you define your own tags designed specifically for your needs. This is a powerful way to store data in a format that can be stored, searched, and shared. Most importantly, since the fundamental format of XML is standardized, if you share or transmit XML across systems or platforms, either locally or over the internet, the recipient can still parse the data due to the standardized XML syntax.

There are many languages based on XML, including XHTML, MathML, SVG, XUL, XBL, RSS, and RDF. You can also define your own.

Structure of an XML document

This sub-article in process writing...

Whole structure XML and XML based languages built on tags.

XML declaration

XML - declaration is not a tag. It is used for the transmission of the meta-data of a document.

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

Attributes:

version  :
Used version XML in this document.
encoding :
Used encoding in this document.

Comments

<!-- Comment -->

"Correct" XML (valid and well-formed)

Сorrect design rules

For an XML document to be correct, the following conditions must be fulfilled:

  • Document must be well-formed.
  • Document must conform to all XML syntax rules.
  • Document must conform to semantic rules, which are usually set in an XML schema or a DTD (Document Type Definition).

Example

<?xml version="1.0" encoding="UTF-8"?>
<message>
    <warning>
        Hello World
    <!--missing </warning> -->
</message>

Now let's look at a corrected version of that same document:

<?xml version="1.0" encoding="UTF-8"?>
<message>
    <warning>
         Hello World
    </warning>
</message>

A document that contains an undefined tag is invalid. For example, if we never defined the <warning> tag, the document above wouldn't be valid.

Most browsers offer a debugger that can identify poorly-formed XML documents.

Entities

Like HTML, XML offers methods (called entities) for referring to some special reserved characters (such as a greater than sign which is used for tags). There are five of these characters that you should know:

EntityCharacterDescription
&lt;<Less than sign
&gt;>Greater than sign
&amp;&Ampersand
&quot;"One double-quotation mark
&apos;'One apostrophe (or single-quotation mark)

Even though there are only 5 declared entities, more can be added using the document's Document Type Definition. For example, to create a new &warning; entity, you can do this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE body [
  <!ENTITY warning "Warning: Something bad happened... please refresh and try again.">
]>
<body>
  <message> &warning; </message>
</body>

You can also use numeric character references to specify special characters; for example, &#xA9; is the "©" symbol.

Displaying XML

XML is usually used for descriptive purposes, but there are ways to display XML data. If you don't define a specific way for the XML to be rendered, the raw XML is displayed in the browser.

One way to style XML output is to specify CSS to apply to the document using the xml-stylesheet processing instruction.

<?xml-stylesheet type="text/css" href="stylesheet.css"?>

There is also another more powerful way to display XML: the Extensible Stylesheet Language Transformations (XSLT) which can be used to transform XML into other languages such as HTML. This makes XML incredibly versatile.

<?xml-stylesheet type="text/xsl" href="transform.xsl"?>

Recommendations

This article is obviously only a very brief introduction to what XML is, with a few small examples and references to get you started. For more details about XML, you should look around on the Web for more in-depth articles.

Learning the HyperText Markup Language (HTML) will help you better understand XML.

See also

The Using XML article above is a great resource on information for transforming and creating your own language.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:162 次

字数:7370

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文