XML是不作为的。也许这有点难以理解,但是XML不会做任何事情。XML被设计用来结构化、存储以及传输信息。下面是John写给George的便签,存储为XML:
<note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>上面的这条便签具有自我描述性。它拥有标题以及留言,同时包含了发送者和接受者的信息。但是,这个XML文档仍然没有做任何事情。它仅仅是包装在XML标签中的纯粹的信息。我们需要编写软件或者程序,才能传送、接收和显示出这个文档。
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>第一行是XML声明。它定义XML的版本(1.0)和所使用的编码(ISO-8859-1 = Latin-1/西欧字符集)。下一行描述文档的根元素(像在说:“本文档是一个便签”)。接下来4行描述根的4个子元素(to, from, heading 以及 body)。最后一行定义根元素的结构。从本例可以设想,该XML文档包含了John给George的一张便签。
<bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>例子中的根元素是<bookstore>。文档中的所有<book>元素都被包含在<bookstore>中。<book>元素有4个子元素<title>、<author>、<year>、<price>。
<note date="08/08/2008"> <to>George</to> <from>John</from> </note>(6)实体引用(Entity Reference)。在XML中,一些字符拥有特殊的意义。如果你把字符 "<" 放在 XML 元素中,会发生错误,这是因为解析器会把它当作新元素的开始。
<note> <to>George</to> <from>John</from> <body>Don't forget the meeting!</body> </note>让我们设想一下,我们创建了一个应用程序,可将<to>、<from>以及<body>元素提取出来,并产生以下的输出:
MESSAGE To: George From: John Don't forget the meeting!想象一下,之后这个XML文档作者又向这个文档添加了一些额外的信息:
<note> <date>2008-08-08</date> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>那么这个应用程序会中断或崩溃吗?不会。这个应用程序仍然可以找到XML文档中的<to>、<from>以及<body>元素,并产生同样的输出。XML的优势之一,就是可以经常在不中断应用程序的情况进行扩展。
<note date="08/08/2008"> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>第二个例子中使用了date元素:
<note> <date>08/08/2008</date> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>第三个例子中使用了扩展的date元素(这是我的最爱):
<note> <date> <day>08</day> <month>08</month> <year>2008</year> </date> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>没有什么规矩可以告诉我们什么时候该使用属性,而什么时候该使用子元素。我的经验是在HTML中,属性用起来很便利,但是在XML中,您应该尽量避免使用属性。如果信息感觉起来很像数据,那么请使用子元素吧。
<note day="08" month="08" year="2008" to="George" from="John" heading="Reminder" body="Don't forget the meeting!"> </note>8、针对元数据的XML属性
<messages> <note id="501"> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note> <note id="502"> <to>John</to> <from>George</from> <heading>Re: Reminder</heading> <body>I will not</body> </note> </messages>上面的ID仅仅是一个标识符,用于标识不同的便签。它并不是便签数据的组成部分。
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE note SYSTEM "Note.dtd"> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>在上例中,DOCTYPE声明是对外部DTD文件的引用(内容参看下面介绍)。
<!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]>10、XML Schema
<xs:element name="note"> <xs:complexType> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>XML Schema描述XML文档的结构。XML Schema语言也称作XML Schema定义(XML Schema Definition,XSD)。XML Schema比DTD更加强大,但语法也更复杂。
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/css" href="cd_catalog.css"?> <CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD> <CD> <TITLE>Hide your heart</TITLE> <ARTIST>Bonnie Tyler</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>CBS Records</COMPANY> <PRICE>9.90</PRICE> <YEAR>1988</YEAR> </CD> . . . . </CATALOG>CSS文件cd_catalog.css的内容如下:
CATALOG { background-color: #ffffff; width: 100%; } CD { display: block; margin-bottom: 30pt; margin-left: 0; } TITLE { color: #FF0000; font-size: 20pt; } ARTIST { color: #0000FF; font-size: 20pt; } COUNTRY,PRICE,YEAR,COMPANY { display: block; color: #000000; margin-left: 20pt; }注意使用CSS格式化XML不是常用的方法,更不能代表XML文档样式化的未来。W3C推荐使用XSLT。
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="simple.xsl"?> <breakfast_menu> <food> <name>Belgian Waffles</name> <price>$5.95</price> <description> two of our famous Belgian Waffles </description> <calories>650</calories> </food> </breakfast_menu>样式表simple.xsl的内容如下:
<?xml version="1.0" encoding="ISO-8859-1"?> <!-- Edited with XML Spy v2007 (http://www.altova.com) --> <html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> <body style="font-family:Arial,helvetica,sans-serif;font-size:12pt; background-color:#EEEEEE"> <xsl:for-each select="breakfast_menu/food"> <div style="background-color:teal;color:white;padding:4px"> <span style="font-weight:bold;color:white"> <xsl:value-of select="name"/></span> - <xsl:value-of select="price"/> </div> <div style="margin-left:20px;margin-bottom:1em;font-size:10pt"> <xsl:value-of select="description"/> <span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving) </span> </div> </xsl:for-each> </body> </html>在这个例子中,XSLT转换是由浏览器完成的,浏览器读取的是XML文件。在使用XSLT来转换XML时,不同的浏览器可能会产生不同结果。为了减少这种问题,可以在服务器上进行XSLT转换(如PHP, JSP, ASP.NET服务器)。
<table> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table>这个XML文档携带有关桌子的信息(一件家具):
<table> <name>African Coffee Table</name> <width>80</width> <length>120</length> </table>假如这两个XML文档被一起使用,由于两个文档都包含带有不同内容和定义的<table>元素,就会发生命名冲突。XML解析器无法确定如何处理这类冲突。
<h:table> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table>此XML文档携带着有关一件家具的信息:
<f:table> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table>现在,命名冲突不存在了,这是由于两个文档都使用了不同的名称来命名它们的<table>元素(<h:table>和<f:table>)。通过使用前缀,我们创建了两种不同类型的<table>元素。这就是命名空间的思想。
<h:table xmlns:h="http://www.w3.org/TR/html4/"> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table>此XML文档携带着有关一件家具的信息:
<f:table xmlns:f="http://www.w3school.com.cn/furniture"> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table>与仅仅使用前缀不同,我们为<table>标签添加了一个xmlns属性,这样就为前缀赋予了一个与某个命名空间相关联的限定名称。
<table xmlns="http://www.w3.org/TR/html4/"> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table>此XML文档携带着有关一件家具的信息:
<table xmlns="http://www.w3school.com.cn/furniture"> <name>African Coffee Table</name> <width>80</width> <length>120</length> </table>当开始使用XSL时,您不久就会看到实际使用中的命名空间。XSL样式表用于将XML文档转换为其他格式,比如HTML。
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr> <th align="left">Title</th> <th align="left">Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>14、XML CDATA
<name> <first>Bill</first> <last>Gates</last> </name>(2)CDATA
<script> <![CDATA[ function matchwo(a,b) { if (a < b && a < 0) then { return 1; } else { return 0; } } ]]> </script>在上面的例子中,解析器会忽略CDATA部分中的所有内容。
<?xml version="1.0" encoding="ISO-8859-1"?> <nitf> <head> <title>Colombia Earthquake</title> </head> <body> <headline> <hl1>143 Dead in Colombia Earthquake</hl1> </headline> <byline> <bytag>By Jared Kotler, Associated Press Writer</bytag> </byline> <dateline> <location>Bogota, Colombia</location> <date>Monday January 25 1999 7:28 ET</date> </dateline> </body> </nitf>(2)XML Weather Service
<?xml version="1.0" encoding="ISO-8859-1" ?> <current_observation> <credit>NOAA's National Weather Service</credit> <credit_URL>http://weather.gov/</credit_URL> <image> <url>http://weather.gov/images/xml_logo.gif</url> <title>NOAA's National Weather Service</title> <link>http://weather.gov</link> </image> <location>New York/John F. Kennedy Intl Airport, NY</location> <station_id>KJFK</station_id> <latitude>40.66</latitude> <longitude>-73.78</longitude> <observation_time_rfc822> Mon, 11 Feb 2008 06:51:00 -0500 EST </observation_time_rfc822> <weather>A Few Clouds</weather> <temp_f>11</temp_f> <temp_c>-12</temp_c> <relative_humidity>36</relative_humidity> <wind_dir>West</wind_dir> <wind_degrees>280</wind_degrees> <wind_mph>18.4</wind_mph> <wind_gust_mph>29</wind_gust_mph> <pressure_mb>1023.6</pressure_mb> <pressure_in>30.23</pressure_in> <dewpoint_f>-11</dewpoint_f> <dewpoint_c>-24</dewpoint_c> <windchill_f>-7</windchill_f> <windchill_c>-22</windchill_c> <visibility_mi>10.00</visibility_mi> <icon_url_base> http://weather.gov/weather/images/fcicons/ </icon_url_base> <icon_url_name>nfew.jpg</icon_url_name> <two_day_history_url> http://www.weather.gov/data/obhistory/KJFK.html </two_day_history_url> <disclaimer_url> http://weather.gov/disclaimer.html </disclaimer_url> <copyright_url> http://weather.gov/disclaimer.html </copyright_url> </current_observation>16、XML标准概览