|
XSLT 实例:统计作者出版物的销量
|
|
| |
|
来源:XSLT.org.cn 作者:XSLT专业站 发布时间:2007-10-27
|
|
XSLT 实例:统计作者出版物的销量
实例中用到的XSLT元素: xsl:stylesheet xsl:template xsl:text xsl:apply-templates xsl:key xsl:output xsl:sort xsl:copy xsl:value-of 实例中用到的XSLT/XPath函数: key sum count position
XML文件代码:
<?xml version="1.0"?> <publisher> <books> <book> <title>Stranger in a strange land</title> <ISBN>0441788386</ISBN> <author-ref ref="rh"/> <sold>230000</sold> </book> <book> <title>Starman Jones</title> <ISBN>0345328116 </ISBN> <author-ref ref="rh"/> <author-ref ref="jldr"/> <sold>80000</sold> </book> </books>
<authors> <author id="rh"> <first_name>Robert</first_name> <last_name>Heinlein</last_name> </author> <author id="jldr"> <first_name>Judy-Lyn</first_name> <last_name>Del Rey</last_name> </author> </authors> </publisher> |
XSLT代码:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:key match="/publisher/books/book" use="author-ref/@ref" name="books-by-author"/> <xsl:output indent="yes" encoding="UTF-16"/> <xsl:template match="/"> <bestsellers-list> <xsl:apply-templates select="/publisher/authors/author"> <xsl:sort data-type="number" order="descending" select="sum(key('books-by-author', @id)/sold)"/> </xsl:apply-templates> </bestsellers-list> </xsl:template>
<xsl:template match="author"> <xsl:copy> <name> <xsl:value-of select="last_name"/>,<xsl:text> </xsl:text> <xsl:value-of select="first_name"/> </name> <total_publications> <xsl:value-of select="count(key('books-by-author', @id))"/> </total_publications> <total_sold> <xsl:value-of select="sum(key('books-by-author', @id)/sold)"/> </total_sold> <rank><xsl:value-of select="position()"/></rank> </xsl:copy> </xsl:template> </xsl:stylesheet> |
输出结果:
<?xml version="1.0" encoding="UTF-8"?> <bestsellers-list> <author> <name>Heinlein, Robert</name> <total_publications>2</total_publications> <total_sold>310000</total_sold> <rank>1</rank> </author> <author> <name>Del Rey, Judy-Lyn</name> <total_publications>1</total_publications> <total_sold>80000</total_sold> <rank>2</rank> </author> </bestsellers-list> |
标题: XSLT 实例:统计作者出版物的销量
关键字:XSLT/XPath函数 key sum count position
|
| 上一篇:XSLT 实例:使用不同的轴(Axis) 下一篇:XSLT 实例:创建带有“前一页”和“后一页”的 HTML 文档 |
|
|
|
|
|