`
禹爸爸
  • 浏览: 80129 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

XML与XSLT

    博客分类:
  • XML
xml 
阅读更多

之前有谈过XML与CSS的搭配,可以让用户拥有多种数据显示样式。但这种搭配亦有不足之处,首先编写者必须了解和掌握XML和CSS两种语法规则,无形中增加了编写者的知识成本;其次,编写者无法对数据进行查询性的显示,即通过XML与CSS的搭配只能将所有数据全部显示出来,无法实现只显示符合某种条件的数据。

为了降低编写者的知识成本,W3C组织又在XML文件基础之上定义了新的样式文件 -- XSLT(eXtended Stylesheet Language Transformation)。它遵循XML语法要求,编写者只要会写XML文件,就能够写出简单的XSLT文件。

<?xmlversion=”1.0”encoding=”GB2312”?>
<xsl:stylesheetversion=”1.0”xmlns:xsl=”http:/www.3w.org/1999/XSL/Transform”>
<!--Formattingcommandsforthisdocumentasawhole-->
</xsl:stylesheet>

XSLT样式文件使用模板(Template)系统来匹配XML文件中的内容,并规定如何处理它们。一旦某个模板匹配上了XML中的一个元素,就会将模板内容应用到该元素上。使用xsl:value-of可以查询出XML中指定结点的值,使用xsl:for-each,xsl:choose、xsl:when和xsl:otherwise,xsl:if元素可以对数据实现循环查询和条件条件。

XSLT样式文件中必须有一个与XML文档根元素匹配的模板。

<?xmlversion="1.0"encoding="utf-8"?>
<xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:templatematch="/">
<html>
<head>
<title>XSLT样式表文件使用举例</title>
</head>
<bodystyle="text-align:center">
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

每个模板以<xsl:template>标记开始,结束与</xsl:template>。其match属性说明该模板将会应用到XML中的哪个元素上去,“/”表示XML文档的根元素。

下面我们用一个具体的例子来说明。假设有一个用于存储用户在网上购物信息的XML文件,其内容如下:

<?xmlversion="1.0"encoding="gb2312"?>
<?xml-stylesheettype=”text/xsl”href=”Test.xsl”?>
<shoppingcart>
<customer>
<name>赵子龙</name>
<email>Jerry@wlw.gor</email>
<zipcode>361021</zipcode>
<address>中国福建省厦门市</address>
</customer>
<shoppingItem>
<item>
<itemNo>1001</itemNo>
<itemName>三国演义</itemName>
<price>30.00</price>
<publisher>文艺出版社</publisher>
</item>
<item>
<itemNo>2001</itemNo>
<itemName>红楼梦</itemName>
<price>120.00</price>
<publisher>文艺出版社</publisher>
</item>
</shoppingItem>
</shoppingcart>

我们为其编写一个XSLT文件,如下:

<?xmlversion=”1.0”encoding=”gb2312”?>
<xsl:styleshetversion=”1.0”xmlns:xsl=”http://www.3w.org/1999/XSL/Transform”>
<xsl:templatematch=”/”>
<html>
<head>
<title>书籍订单</title>
</head>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:templatematch=”shoppingcart”>
<body>
<xsl:apply-templates/>
</body>
</xsl:template>
<xsl:templatematch=”shoopingcart/customer”>
<divstyle=”background-color:teal;font-weight:bold;”>
<xsl:value-ofselect=”name”/>
<xsl:value-ofselect=”email”/>
<xsl:value-ofselect=”zipcode”/>
<xsl:value-ofselect=”address”/>
</div>
</xsl:template>
<xsl:templatematch=”shoppingcart/shoppingItem”>
<tableborder=”1”>
<thead>
<td><b>编号</b></td>
<td><b>书名</b></td>
<td><b>价格</b></td>
<td><b>出版社</b></td>
</thead>
<xsl:for-eachselect=”item”order-by=”-itemNo”>
<tr>
<td><b><xsl:value-ofselect=”itemNo”/></b></td>
<td><b><xsl:value-ofselect=”itemName”/></b></td>
<td><b><xsl:value-ofselect=”price”/></b></td>
<td><b><xsl:value-ofselect=”publisher”/></b></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>

上面的样式文件中共定义了四个模板,分别对应XML文件的根元素、ShoppingCart元素、ShoppingCart/Customer元素、以及ShoppingCart/ShoppingItem元素。

在XML文件中通过

<?xml-stylesheettype=”text/xsl”href=”Test.xsl”?>

将XML文件与欲使用的XSL样式文件关联起来。当浏览器打开XML文件时,XSL解析器会将XSL文件加载,偶后开始遍历XML文件中的所有结点,在每一步都试图将当前结点与模板进行匹配。XSL解析器首先查找根结点,与根结点对应的模板是

<xsl:templatematch=”/”>
<html>
<head>
<title>书籍订单</title>
</head>
<xsl:apply-templates/>
</html>
</xsl:template>

此模板用于创建输出文档的根元素:html和head。在遇到xsl:apply-templates时,解析器移到当前结点的子结点并复复这个过程,即试图将子结点与其他模板匹配。因为当前结点是根结点,它只有一个子结点ShoppingCart,所以解析器找到与ShoppingCart相匹配的模板进行匹配

<xsl:templatematch=”shoppingcart”>
<body>
<xsl:apply-templates/>
</body>
</xsl:template>

根据模板,解析器创建输出文档的Body结点。此时又遇到xsl:apply-templates,因此解析器又查找当前结点的子结点进行模板匹配。ShoppingCart有两个子结点,解析器首先找到与Customer结点匹配的模板,进行转换入理,由此在输出文档中生成顾客的联系信息。这个模板中不含有xsl:apply-templates,因为已经处理了Customer中的所有子结点,无需再向下遍历,所以解析器处理完该结点后,即返回上层结点,然后再查找ShoppingCart结点的其他子结点。

<xsl:templatematch=”shoppingcart/shoppingItem”>
<tableborder=”1”>
<thead>
<td><b>编号</b></td>
<td><b>书名</b></td>
<td><b>价格</b></td>
<td><b>出版社</b></td>
</thead>
<xsl:for-eachselect=”item”order-by=”-itemNo”>
<tr>
<td><b><xsl:value-ofselect=”itemNo”/></b></td>
<td><b><xsl:value-ofselect=”itemName”/></b></td>
<td><b><xsl:value-ofselect=”price”/></b></td>
<td><b><xsl:value-ofselect=”publisher”/></b></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>

这个模板在输出文档中列出顾客订购的书籍清单,由于顾客可能一次订购多本书籍,故使用了xsl:for-each进行了循环处理,并为指定书籍按其序列号进行排列。

到此,完成整个XML文档的转换并输出。

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics