📄 1_xml.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><!--NewPage--><html><head><title>A Quick Introduction to XML</title><style type="text/css"><!----></style><meta name="fileID" content="quickIntro"><meta name="keywords" content="XML"></head><body BGCOLOR="#ffffff"><table width="100%"><tr> <td align=left> <a href="index.html"><img src="../images/PreviousArrow.gif" width=26 height=26 align=bottom border=0 alt="Previous | "></a><ahref="2_specs.html"><img src="../images/NextArrow.gif" width=26 height=26 align=bottom border=0 alt="Next | "></a><a href="../alphaIndex.html"><img src="../images/xml_IDX.gif" width=26 height=26 align=bottom border=0 alt="Index | "></a><a href="../TOC.html"><imgsrc="../images/xml_TOC.gif" width=26 height=26 align=bottom border=0 alt="TOC | "></a><a href="../index.html"><imgsrc="../images/xml_Top.gif" width=26 height=26 align=bottom border=0 alt="Top | "></a> </td> <td align=right><strong><em><a href="index.html">Top</a></em></strong> <a href="../TOC.html#intro"><strong><em></em></strong></a> <a href="../TOC.html#intro"><strong><em>Contents</em></strong></a> <a href="../alphaIndex.html"><strong><em>Index</em></strong></a> <a href="../glossary.html"><strong><em>Glossary</em></strong></a></td></tr></table><p><center> <IMG SRC="../images/shoeline2.gif" ALIGN="BOTTOM" BORDER="0" WIDTH="202" HEIGHT="25" NATURALSIZEFLAG="3"> <IMG SRC="../images/shoeline2.gif" ALIGN="BOTTOM" BORDER="0" WIDTH="202" HEIGHT="25" NATURALSIZEFLAG="3"> </center> <blockquote> <blockquote> <hr size=4> </blockquote> </blockquote><p> <h2> 1. A Quick Introduction to XML</h2><table width="40%" border="1" align="right"> <tr> <td> <div align="center"><b><i>Link Summary</i></b></div> </td> </tr> <tr> <td> <dl> <dt><b><i>Local Links</i></b></dt> </dl> <ul> <li><a href="2_specs.html">XML and Related Specs</a></li> <li><a href="4_design.html">Designing an XML Data Structure</a></li> <li><a href="2_specs.html#RDF">RDF</a></li> <li><a href="2_specs.html#XSL">XSL</a><br> </li> </ul> <dl> <dt><i><b>External Links</b></i> </dt> </dl> <ul> <li><a href="http://www.ucc.ie/xml/">XML FAQ</a></li> <li><a href="http://www.w3.org/XML/">XML Info and Recommended Reading</a></li> <li><a href="http://www.oasis-open.org/cover/">SGML/XML Web Page</a></li> <li><a href="http://www.sciam.com/1999/0599issue/0599bosak.html">Scientific American article</a></li> </ul> <dl> <dt><b><i>Glossary Terms</i></b></dt> <dd><a href="../glossary.html#attribute">attributes</a>, <a href="../glossary.html#declaration">declaration</a>, <a href="2_specs.html#DTD">DTD</a>, <a href="../glossary.html#element">element</a>, <a href="../glossary.html#entity">entity</a>, <a href="../glossary.html#prolog">prolog</a>, <a href="../glossary.html#tag">tag</a>, <a href="../glossary.html#wellFormed">well-formed</a> </dd> </dl> </td> </tr></table><p> This page covers the basics of XML. The goal is to give you just enough information to get started, so you understand what XML is all about. (You'll learn about XML in later sections of the tutorial.) We then outline the major features that make XML great for information storage and interchange, and give you a general idea of how XML can be used. This section of the tutorial covers: <ul> <li> <a href="#intro">What Is XML?</a> </li> <li> <a href="#cool">Why Is XML Important?</a> </li> <li><a href="#uses">How Can You Use XML?</a> </li></ul><h3><a name="intro"></a>What Is XML?</h3><blockquote> <p>XML is a text-based markup language that is fast becoming the standard for data interchange on the Web. As with HTML, you identify data using <a href="../glossary.html#tag">tag</a>s (identifiers enclosed in angle brackets, like this: <...>). Collectively, the tags are known as "markup".</p> <p>But unlike HTML, XML tags tell you what the data <i>means</i>, rather than how to display it. Where an HTML tag says something like "display this data in bold font" (<code><b>...</b></code>), an XML tag acts like a field name in your program. It puts a label on a piece of data that identifies it (for example: <code><message>...</message></code>).</p> <p>In the same way that you define the field names for a data structure, you are free to use any XML tags that make sense for a given application. Naturally, though, for multiple applications to use the same XML data, they have to agree on the tag names they intend to use.</p> <p>Here is an example of some XML data you might use for a messaging application:</p> <pre><b><message></b> <b><to></b>you@yourAddress.com<b></to></b> <b><from></b>me@myAddress.com<b></from></b> <b><subject></b>XML Is Really Cool<b></subject></b> <b><text></b> How many ways is XML cool? Let me count the ways... <b></text></b><b></message></b></pre> <blockquote> <p><b>Note: </b>Throughout this tutorial, we use boldface text to highlight things we want to bring to your attention. XML does not require anything to be in bold!</p> </blockquote></blockquote><blockquote> <p>The tags in this example identify the message as a whole, the destination and sender addresses, the subject, and the text of the message. As in HTML, the <code><to></code> tag has a matching end tag: <code></to></code>. The data between the tag and and its matching end tag defines an <a href="../glossary.html#element">element</a> of the XML data. Note, too, that the content of the <code><to></code> tag is entirely contained within the scope of the <code><message>..</message></code> tag. It is this ability for one tag to contain others that gives XML its ability to represent hierarchical data structures</p> <p>Once again, as with HTML, whitespace is essentially irrelevant, so you can format the data for readability and yet still process it easily with a program. Unlike HTML, however, in XML you could easily search a data set for messages containing "cool" in the subject, because the XML tags identify the content of the data, rather than specifying its representation. </p></blockquote><h4><a name="tags"></a>Tags and Attributes</h4><blockquote> <p>Tags can also contain <a href="../glossary.html#attribute">attributes</a> -- additional information included as part of the tag itself, within the tag's angle brackets. The following example shows an email message structure that uses attributes for the "to", "from", and "subject" fields:</p> <pre><message <b>to="</b>you@yourAddress.com<b>"</b><b> from=</b>"me@myAddress.com<b>"</b> <b>subject="</b>XML Is Really Cool<b>"</b>> <br> <text> How many ways is XML cool? Let me count the ways... </text></message></pre> <p></p> <p>As in HTML, the attribute name is followed by an equal sign and the attribute value, and multiple attributes are separated by spaces. Unlike HTML, however, in XML commas between attributes are not ignored -- if present, they generate an error. </p> <p>Since you could design a data structure like <code><message></code> equally well using either attributes or tags, it can take a considerable amount of thought to figure out which design is best for your purposes. The last part of this tutorial, <a href="4_design.html">Designing an XML Data Structure</a>, includes ideas to help you decide when to use attributes and when to use tags.</p></blockquote><h4><a name="empty"></a>Empty Tags</h4><blockquote> <p>One really big difference between XML and HTML is that an XML document is always constrained to be <a href="4_design.html">well formed</a>. There are several rules that determine when a document is well-formed, but one of the most important is that every tag has a closing tag. So, in XML, the <code></to></code> tag is not optional. The <code><to></code> element is never terminated by any tag other than <code></to></code>. </p> <blockquote> <p><b><a name="nesting"></a>Note: </b>Another important aspect of a well-formed document is that all tags are completely nested. So you can have <code><message>..<to>..</to>..</message></code>, but never <code><message>..<to>..</message>..</to></code>. A complete list of requirements is contained in the list of XML Frequently Asked Questions (FAQ) at <a href="http://www.ucc.ie/xml/#FAQ-VALIDWF"><code>http://www.ucc.ie/xml/#FAQ-VALIDWF</code></a>. (This FAQ is on the w3c "Recommended Reading" list at <a href="http://www.w3.org/XML/"><code>http://www.w3.org/XML/</code></a>.)</p> </blockquote> <p>Sometimes, though, it makes sense to have a tag that stands by itself. For example, you might want to add a "flag" tag that marks message as important. A tag like that doesn't enclose any content, so it's known as an "empty tag". You can create an empty tag by ending it with <code>/></code> instead of <code>></code>. For example, the following message contains such a tag:</p> <pre><message to="you@yourAddress.com" from="me@myAddress.com" subject="XML Is Really Cool"> <b><flag/></b> <br> <text> How many ways is XML cool? Let me count the ways... </text></message></pre> <b>Note: </b>The empty tag saves you from having to code <code><flag></flag></code> in order to have a well-formed document. You can control which tags are allowed to be empty by creating a Document Type Definition, or <a href="2_specs.html#DTD">DTD</a>. We'll talk about that in a few moments. If there is no DTD, then the document can contain any kinds of tags you want, as long as the document is well-formed. <br> <p></p> <p></p></blockquote><h4><a name="comments"></a>Comments in XML Files</h4><blockquote> <p>XML comments look just like HTML comments:</p> <pre><message to="you@yourAddress.com" from="me@myAddress.com" subject="XML Is Really Cool"> <b><!-- This is a comment --></b> <text> How many ways is XML cool? Let me count the ways... </text></message></pre> <p></p></blockquote><p></p><h4><a name="prolog"></a>The XML Prolog</h4><blockquote> <p>To complete this journeyman's introduction to XML, note that an XML file always starts with a <a href="../glossary.html#prolog">prolog</a>. The minimal prolog contains a <a href="../glossary.html#declaration">declaration</a> that identifies the document as an XML document, like this:</p> <pre><?xml version="1.0"?></pre> <p>The declaration may also contain additional information, like this:</p> <pre><?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?></pre> <p>The XML declaration is essentially the same as the HTML header, <code><html></code>, except that it uses <code><?..?></code> and it may contain the following attributes:</p> <dl> <dl> <dt><b>version</b></dt> <dd>Identifies the version of the XML markup language used in the data. This attribute is not optional.</dd> <dt><b>encoding</b></dt> <dd>Identifies the character set used to encode the data. "ISO-8859-1" is "Latin-1" the Western European and English language character set. (The default is compressed Unicode: UTF-8.)</dd> <dt><b>standalone</b></dt> <dd>Tells whether or not this document references an external <a href="../glossary.html#entity">entity</a> or an external data type specification (see below). If there are no external references, then "yes" is appropriate</dd> </dl> </dl> <p>The prolog can also contain definitions of <a href="../glossary.html#entity">entities</a> (items that are inserted when you reference them from within the document) and specifications that tell which tags are valid in the document, both declared in a Document Type Definition (<a href="2_specs.html#DTD">DTD</a>) that can be defined directly within the prolog, as well as with pointers to external specification files. But those are the subject of later tutorials. For more
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -