<
book
>
< title > XML 指南 </ title >
< prod id ="33-657" media ="paper" ></ prod >< chapter > XML入门简介
< para > 什么是HTML </ para >
< para > 什么是XML </ para >
</ chapter >
< chapter > XML语法
< para > XML元素必须有结束标记 </ para >
< para > XML元素必须正确的嵌套 </ para >
</ chapter >
</ book >
< title > XML 指南 </ title >
< prod id ="33-657" media ="paper" ></ prod >< chapter > XML入门简介
< para > 什么是HTML </ para >
< para > 什么是XML </ para >
</ chapter >
< chapter > XML语法
< para > XML元素必须有结束标记 </ para >
< para > XML元素必须正确的嵌套 </ para >
</ chapter >
</ book >
在上面的例子中,book元素有元素内容,因为book元素包含了其它的元素。Chapter元素有混合内容,因为它里面包含了文本和其他元素。para元素有简单的内容,因为它里面仅有简单的文本。prod元素有空内容,因为他不携带任何信息。
在上面的例子中,只有prod元素有属性,id属性值是33-657,media属性值是paper。
XML元素命名必须遵守下面的规则:
- 元素的名字可以包含字母,数字和其他字符。
- 元素的名字不能以数字或者标点符号开头。
- 元素的名字不能以XML(或者xml,Xml,xMl...)开头。
- 元素的名字不能包含空格。
属性值必须用引号引着。单引号、双引号都可以使用。
数据既可以存储在子元素中也可以存储在属性中。
<
person
sex
="female"
>
< firstname > Anna </ firstname >
< lastname > Smith </ lastname >
</ person >
< person >
< sex > female </ sex >
< firstname > Anna </ firstname >
< lastname > Smith </ lastname >
</ person >
< firstname > Anna </ firstname >
< lastname > Smith </ lastname >
</ person >
< person >
< sex > female </ sex >
< firstname > Anna </ firstname >
< lastname > Smith </ lastname >
</ person >
什么时候用属性,什么时候用子元素没有一个现成的规则可以遵循。我的经验是属性在HTML中可能相当便利,但在XML中,你最好避免使用。
- 属性不能包含多个值(子元素可以)。
- 属性不容易扩展。
- 属性不能够描述结构(子元素可以)。
- 属性很难被程序代码处理。
- 属性值很难通过DTD进行测试。
仅使用属性来描述那些与数据关系不大的额外信息。
元数据(与数据有关的数据)应该以属性的方式存储,而数据本身应该以
<
messages
>
< note ID ="501" >
< to > Tove </ to >
< from > Jani </ from >
< heading > Reminder </ heading >
< body > Don't forget me this weekend! </ body >
</ note >
< note ID ="502" >
< to > Jani </ to >
< from > Tove </ from >
< heading > Re: Reminder </ heading >
< body > I will not! </ body >
</ note >
</ messages >
< note ID ="501" >
< to > Tove </ to >
< from > Jani </ from >
< heading > Reminder </ heading >
< body > Don't forget me this weekend! </ body >
</ note >
< note ID ="502" >
< to > Jani </ to >
< from > Tove </ from >
< heading > Re: Reminder </ heading >
< body > I will not! </ body >
</ note >
</ messages >
一个 xml文件:
http://www.w3schools.com/xml/cd_catalog.xml
<?
xml version="1.0" encoding="ISO-8859-1"
?>
<!-- Edited with XML Spy v2007 (http://www.altova.com) -->
< 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 >
< CD >
< TITLE > Greatest Hits </ TITLE >
< ARTIST > Dolly Parton </ ARTIST >
< COUNTRY > USA </ COUNTRY >
< COMPANY > RCA </ COMPANY >
< PRICE > 9.90 </ PRICE >
< YEAR > 1982 </ YEAR >
</ CD >
</ CATALOG >
<!-- Edited with XML Spy v2007 (http://www.altova.com) -->
< 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 >
< CD >
< TITLE > Greatest Hits </ TITLE >
< ARTIST > Dolly Parton </ ARTIST >
< COUNTRY > USA </ COUNTRY >
< COMPANY > RCA </ COMPANY >
< PRICE > 9.90 </ PRICE >
< YEAR > 1982 </ YEAR >
</ CD >
</ CATALOG >
另一个例子:http://www.w3schools.com/xml/plant_catalog.xml
<?
xml version="1.0" encoding="ISO-8859-1"
?>
<!-- Edited with XML Spy v2007 (http://www.altova.com) -->
< CATALOG >
< PLANT >
< COMMON > Bloodroot </ COMMON >
< BOTANICAL > Sanguinaria canadensis </ BOTANICAL >
< ZONE > 4 </ ZONE >
< LIGHT > Mostly Shady </ LIGHT >
< PRICE > $2.44 </ PRICE >
< AVAILABILITY > 031599 </ AVAILABILITY >
</ PLANT >
< PLANT >
< COMMON > Columbine </ COMMON >
< BOTANICAL > Aquilegia canadensis </ BOTANICAL >
< ZONE > 3 </ ZONE >
< LIGHT > Mostly Shady </ LIGHT >
< PRICE > $9.37 </ PRICE >
< AVAILABILITY > 030699 </ AVAILABILITY >
</ PLANT >
< PLANT >
< COMMON > Marsh Marigold </ COMMON >
< BOTANICAL > Caltha palustris </ BOTANICAL >
< ZONE > 4 </ ZONE >
< LIGHT > Mostly Sunny </ LIGHT >
< PRICE > $6.81 </ PRICE >
< AVAILABILITY > 051799 </ AVAILABILITY >
</ PLANT >
</ CATALOG >
<!-- Edited with XML Spy v2007 (http://www.altova.com) -->
< CATALOG >
< PLANT >
< COMMON > Bloodroot </ COMMON >
< BOTANICAL > Sanguinaria canadensis </ BOTANICAL >
< ZONE > 4 </ ZONE >
< LIGHT > Mostly Shady </ LIGHT >
< PRICE > $2.44 </ PRICE >
< AVAILABILITY > 031599 </ AVAILABILITY >
</ PLANT >
< PLANT >
< COMMON > Columbine </ COMMON >
< BOTANICAL > Aquilegia canadensis </ BOTANICAL >
< ZONE > 3 </ ZONE >
< LIGHT > Mostly Shady </ LIGHT >
< PRICE > $9.37 </ PRICE >
< AVAILABILITY > 030699 </ AVAILABILITY >
</ PLANT >
< PLANT >
< COMMON > Marsh Marigold </ COMMON >
< BOTANICAL > Caltha palustris </ BOTANICAL >
< ZONE > 4 </ ZONE >
< LIGHT > Mostly Sunny </ LIGHT >
< PRICE > $6.81 </ PRICE >
< AVAILABILITY > 051799 </ AVAILABILITY >
</ PLANT >
</ 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;
}
{...} {
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;
}
格式文件:
<?
xml version="1.0" encoding="ISO-8859-1"
?>
<!-- Edited with XML Spy v4.2 -->
< html xsl:version ="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" xmlns ="http://www.w3.org/TR/xhtml1/strict" >
< 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 >
<!-- Edited with XML Spy v4.2 -->
< html xsl:version ="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" xmlns ="http://www.w3.org/TR/xhtml1/strict" >
< 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 >
xml原文件:
<?
xml version="1.0" encoding="ISO-8859-1"
?>
<!-- Edited with XML Spy v4.2 -->
<? 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 with plenty of real maple syrup </ description >
< calories > 650 </ calories >
</ food >
< food >
< name > Strawberry Belgian Waffles </ name >
< price > $7.95 </ price >
< description > light Belgian waffles covered with strawberries and whipped cream </ description >
< calories > 900 </ calories >
</ food >
</ breakfast_menu >
<!-- Edited with XML Spy v4.2 -->
<? 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 with plenty of real maple syrup </ description >
< calories > 650 </ calories >
</ food >
< food >
< name > Strawberry Belgian Waffles </ name >
< price > $7.95 </ price >
< description > light Belgian waffles covered with strawberries and whipped cream </ description >
< calories > 900 </ calories >
</ food >
</ breakfast_menu >
运行xml原文件效果如下:
Belgian Waffles$5.95
two of our famous Belgian Waffles with plenty of real maple syrup
(650 calories per serving)
Strawberry Belgian Waffles$7.95
light Belgian waffles covered with strawberries and whipped cream
(900 calories per serving)
一个HTML的表格绑定到此数据岛上:
<
html
>
< body >
< xml id ="cdcat" src ="cd_catalog.xml" tppabs ="http://www.w3schools.com/xml/cd_catalog.xml" ></ xml >
< table border ="1" datasrc ="#cdcat" >
< tr >
< td >< div datafld ="ARTIST" ></ div ></ td >
< td >< div datafld ="TITLE" ></ div ></ td >
</ tr >
</ table >
</ body >
</ html >
< body >
< xml id ="cdcat" src ="cd_catalog.xml" tppabs ="http://www.w3schools.com/xml/cd_catalog.xml" ></ xml >
< table border ="1" datasrc ="#cdcat" >
< tr >
< td >< div datafld ="ARTIST" ></ div ></ td >
< td >< div datafld ="TITLE" ></ div ></ td >
</ tr >
</ table >
</ body >
</ html >
<
script type
=
"
text/javascript
"
>
var xmlDoc = new ActiveXObject( " Microsoft.XMLDOM " )
xmlDoc.async = " false "
xmlDoc.load( " note.xml " ) // 方法名称是load
// ....... processing the document goes here
</ script >
< script type = " text/javascript " >
var text = " <note> "
text = text + " <to>Tove</to><from>Jani</from> "
text = text + " <heading>Reminder</heading> "
text = text + " <body>Don't forget me this weekend!</body> "
text = text + " </note> "
var xmlDoc = new ActiveXObject( " Microsoft.XMLDOM " )
xmlDoc.async = " false "
xmlDoc.loadXML(text) // 方法名称是loadXML
// ....... processing the document goes here
</ script >
var xmlDoc = new ActiveXObject( " Microsoft.XMLDOM " )
xmlDoc.async = " false "
xmlDoc.load( " note.xml " ) // 方法名称是load
// ....... processing the document goes here
</ script >
< script type = " text/javascript " >
var text = " <note> "
text = text + " <to>Tove</to><from>Jani</from> "
text = text + " <heading>Reminder</heading> "
text = text + " <body>Don't forget me this weekend!</body> "
text = text + " </note> "
var xmlDoc = new ActiveXObject( " Microsoft.XMLDOM " )
xmlDoc.async = " false "
xmlDoc.loadXML(text) // 方法名称是loadXML
// ....... processing the document goes here
</ script >
<
html
>
< head >
< script type ="text/javascript"
for ="window" event ="onload" > ...
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("xml_note.xml")
nodes=xmlDoc.documentElement.childNodes
to.innerText= nodes.item(0).text
from.innerText= nodes.item(1).text
header.innerText=nodes.item(2).text
body.innerText= nodes.item(3).text
</ script >
< title > HTML using XML data </ title >
</ head >
< body bgcolor ="yellow" >
< h1 > W3Schools.com Internal Note </ h1 >
< b > To: </ b >
< span id ="to" > </ span >
< br />
< b > From: </ b >
< span id ="from" ></ span >
< hr >
< b >< span id ="header" ></ span ></ b >
< hr >
< span id ="body" ></ span >
</ body >
</ html >
< head >
< script type ="text/javascript"
for ="window" event ="onload" > ...
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("xml_note.xml")
nodes=xmlDoc.documentElement.childNodes
to.innerText= nodes.item(0).text
from.innerText= nodes.item(1).text
header.innerText=nodes.item(2).text
body.innerText= nodes.item(3).text
</ script >
< title > HTML using XML data </ title >
</ head >
< body bgcolor ="yellow" >
< h1 > W3Schools.com Internal Note </ h1 >
< b > To: </ b >
< span id ="to" > </ span >
< br />
< b > From: </ b >
< span id ="from" ></ span >
< hr >
< b >< span id ="header" ></ span ></ b >
< hr >
< span id ="body" ></ span >
</ body >
</ html >
使用前缀解决命名冲突问题
下面的XML文档在table元素中携带了信息:
< h:table >
< h:tr >
< h:td > Apples </ h:td >
< h:td > Bananas </ h:td >
</ h:tr >
</ h:table >
下面的XML文档携带了家具table的信息:
< f:table >
< f:name > African Coffee Table </ f:name >
< f:width > 80 </ f:width >
< f:length > 120 </ f:length >
</ f:table >
下面的XML文档在table元素中携带了信息:
< h:table >
< h:tr >
< h:td > Apples </ h:td >
< h:td > Bananas </ h:td >
</ h:tr >
</ h:table >
下面的XML文档携带了家具table的信息:
< f:table >
< f:name > African Coffee Table </ f:name >
< f:width > 80 </ f:width >
< f:length > 120 </ f:length >
</ f:table >
使用命名空间
下面的XML文档在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文档携带了家具table的信息:
< f:table xmlns:f ="http://www.w3schools.com/furniture" >
< f:name > African Coffee Table </ f:name >
< f:width > 80 </ f:width >
< f:length > 120 </ f:length >
</ f:table >
下面的XML文档在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文档携带了家具table的信息:
< f:table xmlns:f ="http://www.w3schools.com/furniture" >
< f:name > African Coffee Table </ f:name >
< f:width > 80 </ f:width >
< f:length > 120 </ f:length >
</ f:table >
<
message
>
if salary
<
1000
then</message
>
为了避免出现这种情况,必须将字符" < " 转换成实体,象下面这样:
<message > if salary < 1000 then </ message >
下面是五个在XML文档中预定义好的实体:
< < 小于号
> ; > 大于号
& & 和
' ' 单引号
" " 双引号
实体必须以符号"&"开头,以符号";"结尾。
注意: 只有" < " 字符和"&"字符对于XML 来说是严格禁止使用的。剩下的都是合法的,为了减少出错,使用实体是一个好习惯。
为了避免出现这种情况,必须将字符" < " 转换成实体,象下面这样:
<message > if salary < 1000 then </ message >
下面是五个在XML文档中预定义好的实体:
< < 小于号
> ; > 大于号
& & 和
' ' 单引号
" " 双引号
实体必须以符号"&"开头,以符号";"结尾。
注意: 只有" < " 字符和"&"字符对于XML 来说是严格禁止使用的。剩下的都是合法的,为了减少出错,使用实体是一个好习惯。
在CDATA内部的所有内容都会被解析器忽略。
如果文本包含了很多的"<"字符和"&"字符——就象程序代码一样,那么最好把他们都放到CDATA部件中。
一个 CDATA 部件以"<![CDATA[" 标记开始,以"]]>"标记结束:不能嵌套
<script> <![CDATA[ function matchwo(a,b) { if (a < b && a < 0) then { return 1 } else { return 0 } } ]]> </script> |
在前面的例子中,所有在CDATA部件之间的文本都会被解析器忽略。