XSLT 2.0应用实践

时间:2007-10-11 10:14:08  来源:开发者在线  作者:

在安装了 AltovaXML 2007 之后,使用 XSLT2.0 起来也很简单,只需要添加一个引用即可,如图片 C 所示。

图片 C ——添加一个引用

现在预备知识已经讲解完了,我们现在来创建一个 C# 程序,列表 A 是 XML 文档,列表 B 是 XSLT 文档,列表 C 是我们使用的 C# 程序代码。

列表 A —— XML 文档

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

< world >

<country name="Canada" continent="North America">

< city> Toronto</city>

< city> Vancouver</city>

</country>

<country name="Jamaica" continent="North America">

< city> Kingston</city>

< city> Ocho Rios</city>

</country>

<country name="United States" continent="North America">

< city> Allentown</city>

< city> Mobile</city>

</country>

<country name="United Kingdom" continent="Europe">

< city> London</city>

< city> Dundee</city>

</country>

<country name="France" continent="Europe">

< city> Paris</city>

< city> Nice</city>

</country>

<country name="Japan" continent="Asia">

< city> Tokyo</city>

< city> Osaka</city>

</country>

</world>

列表 B —— XSLT2.0 样式表

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

< xsl:stylesheet version="2.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:fo="

http://www.w3.org/1999/XSL/Format"xmlns:xs="http://www.w3.org/2001/XMLSchema"xmlns:fn="

http://www.w3.org/2005/xpath-functions"xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">

< xsl :template match="/">

< xsl :element name="world">

< xsl :for -each-group select="//country" group-by="@continent">

< xsl :sort select="@continent" data-type="text" order="ascending"/>

< xsl :variable name="continent" select="@continent"/>

< xsl :apply -templates select="//country[@continent = $continent]" mode="group">

< xsl :sort select="@name" data-type="text" order="ascending"/>

</ xsl :apply -templates>

</ xsl :for -each-group>

</ xsl :element >

</ xsl :template >

< xsl :template match="*" mode="group">

< xsl :copy -of select="."/>

</ xsl :template >

</ xsl :stylesheet >

文章评论

共有 位CH网友发表了评论 查看完整内容