A XSLT Sample<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
(Wang hailong)
在中文网站到看到了不少关于XSLT的例子,可是大部分都属于入门性质的。下面给出一个XSLT的例子,说明XSLT的一些典型的用法。
XSLT1.0规范定义了document()函数,提供了初步的处理多个xml输入文件的功能。我们用这个功能来实现新旧xml文件的比较。
比如,我们有一个xml格式产品列表,列出一些关于XSLT处理的(Open Source)软件。
每隔一段时间,我们就更新一次产品列表。
下面的Product.1.xml是第一个产品列表。
product.1.xml
<?xml version="1.0" ?>
<product-root>
<!-- no histroy yet -->
<product-history/>
<product>
<product-id>001</product-id>
<product-name>Apache Xalan xslt</product-name>
<url>http://xml.apache.org/xalan-j/</url>
</product>
<product>
<product-id>002</product-id>
<product-name>saxon xslt</product-name>
<url>http://saxon.sourceforge.net/</url>
</product>
</product-root>
过了一段时间,产品列表更新为Product.2.xml。其中的product-history元素纪录以前的产品列表——Product.1.xml。
product.2.xml
<?xml version="1.0" ?>
<product-root>
<!-- refer to last product list -->
<product-history>product.1.xml</product-history>
<product>
<product-id>001</product-id>
<product-name>Apache Xalan xslt</product-name>
<url>http://xml.apache.org/xalan-j/</url>
</product>
<product>
<product-id>002</product-id>
<product-name>saxon xslt</product-name>
<url>http://saxon.sourceforge.net/</url>
</product>
<product>
<product-id>003</product-id>
<product-name>XT xslt</product-name>
<url>http://www.4xt.org/</url>
</product>
<product>
<product-id>004</product-id>
<product-name>oasis xslt</product-name>
<url>http://www.oasis-open.org/</url>
</product>
</product-root>
我们用下面的xsl文件处理product.2.xml,查找新增加的product。
product.diff.xsl
<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="product-root">
<difference>
<!-- get all history product-->
<xsl:variable name="history" select="document(product-history)//product" />
<!-- copy the product which is not in product history -->
<xsl:copy-of select="product[not(product-id=$history/product-id)]"/>
</difference>
</xsl:template>
</xsl:transform>
这个XSL文件虽然短小,却包括了XSLT很重要的一些特性和XPath的很典型的用法。因为product-history的内容是product.1.xml,所以document(product-history)取得上次产品列表product.1.xml的根元素。
document(product-history)//product取得product.1.xml的根元素下面所有的product元素。我们也可以写成document(product-history)/product-root/product,这种写法更加确定,指定只选取product-root元素下面的product。
注意,product[not(product-id=$history/product-id)]表示“product-id和history product-id都不相同的product”;product[product-id!=$history/product-id]表示“product-id 和history product-id至少有一个不相同的product”。
处理后的xml结果如下。
<?xml version="1.0" encoding="UTF-8"?>
<difference>
<product>
<product-id>003</product-id>
<product-name>XT xslt</product-name>
<url>http://www.4xt.org/</url>
</product>
<product>
<product-id>004</product-id>
<product-name>oasis xslt</product-name>
<url>http://www.oasis-open.org/</url>
</product>
</difference>
关于如何运行这个例子或其它的XSLT例子。首先,您需要一个XSLT转换工具。哪里获得XSLT转换工具呢?参见上面的产品列表product.2.xml,里面就包括了很好的XSLT转换工具。访问里面的url。:-)
分享到:
相关推荐
Learning XSLT moves smoothly from the simple ... Learning XSLT also explains how the XML Path Language (XPath) is used by XSLT and provides a glimpse of what the future holds for XSLT 2.0 and XPath 2.0.
xslt手册.chm java实现xslt 简单的 Xalan 扩展函数 - 工程 xslt知识点速查手册.doc 2 Javascript XSLT 处理XML文件(IE and Firefox).doc Saxon 剖析 XSLT 处理器.doc
Extensible Stylesheet Language Transformations (XSLT) is a language for transforming XML documents and data from one format to another. Answering the demand for an introductory book on XSLT processing...
This book, as the title implies, is primarily a practical reference book for professional XSLT developers. It assumes no previous knowledge of the language, and many developers have used it as their ...
XSLT 元素使用说明
testng-xslt-1.1.2.zip
xslt中英文对照教程 xslt中英文对照教程 xslt中英文对照教程 XSLT 是一种用于转换 XML 文档的语言。
xslt帮助手册,非常使用
XSLT 元素 XSLT在服务器上 XSLT在客户端
javascript xml xsl xslt javascript xml xsl xslt javascript xml xsl xslt javascript xml xsl xslt
xslt中调用java
xslt语法参考手册 详细地介绍了XSLT的语法,我就是看着这本书学的,发上来和大家一起分享,非常实用。
主要用于练习xslt,初学者可以参考一下
xslt中文参考手册
XSLT使用的例子源码,XML与XSLT结合实现页面格式与数据的分离
XSLT标准参考手册,格式是chm格式的 使用学习很方便的。
java_xslt转换xml
xslt 扩展 exslt PDF教程
Beginning XSLT and Xpath 高清英文原版 PDF
xslt深度历险 xslt深度历险