精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>XML>>(2)XML核心规范>>(2)XSL>>XSLT例子二

主题:XSLT例子二
发信人: mysunshine()
整理人: leitiger(2001-06-27 15:25:36), 站内信件
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"/>
<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>





----
$$      $$$$$$    
$$        $$    
$$        $$    
$$        $$     
$$        $$
$$$$$$  $$$      

[关闭][返回]