精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>XML>>(2)XML核心规范>>(2)XSL>>XSLT例子一:联合交叉两个nodesets

主题:XSLT例子一:联合交叉两个nodesets
发信人: mysunshine()
整理人: leitiger(2001-06-27 15:25:36), 站内信件
XSLT例子一:联合交叉两个nodesets
    使用XSLT元素:xsl:stylesheet   xsl:template   xsl:variable    xsl:value-of
    使用XSLT 和 Xpath的函数: count
    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>
<book>
  <title>The Space Merchants</title>
  <ISBN>02352123456 </ISBN>
  <author-ref ref="fp"/>
  <author-ref ref="ck"/>
  <sold>120000</sold>
</book>
</books>
<authors>
  <author id="rh">
    <first_name>Robert</first_name>
    <last_name>Heinlein</last_name>
  </author>
  <author id="ck">
    <first_name>Cyril</first_name>
    <last_name>Kornbluth</last_name>
  </author>
  <author id="fp">
    <first_name>Frederick</first_name>
    <last_name>Pohl</last_name>
  </author>
  <author id="jldr">
    <first_name>Judy-Lyn</first_name>
    <last_name>Del Rey</last_name>
  </author>
</authors>
</publisher>

XSLT代码:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="books_by_rh" select="//book[author-ref/@ref='rh']"/>
<xsl:variable name="books_by_more_than_one_author" select="//book[count(author-ref) > 1]"/>
<xsl:template match="/">
    Total number of books by Robert Heinlein: <xsl:value-of select="count($books_by_rh)" />
    Total number of books by several authors: <xsl:value-of select="count($books_by_more_than_one_author)" />
    Number of books in the union of the two sets <xsl:value-of
select="count($books_by_rh|$books_by_more_than_one_author)"/> 
    Number of books in the intersection of the two sets <xsl:value-of
select="count($books_by_rh[count(.|$books_by_more_than_one_author)=
count($books_by_more_than_one_author)])"/>
</xsl:template>
</xsl:stylesheet>





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

[关闭][返回]