如何在XSLT中汇总值?

问题描述 投票:0回答:1

这是我的xml:

<countries>
   <country>
      <name> .....</name>
      <capital>....</capital>
      <continent>....</continent>
   </country>
   .
   .
   .(a lot of countries)..
<countries>

并且我创建了一个带有2列的html表(使用xslt),分别表示每个国家的名称和首都。但是现在我想为每个大陆创建一个表,并且每个表都包含该大陆所有国家的清单,而我不知道该如何进行!谢谢你的帮助!这是我的XSLT的快速视图:

<table border="3" width="100%" align="center">
<tr>
    <th>Name</th>
    <th>Capital</th>
</tr>

<xsl:for-each select="countries/country">
<tr>
<td >
    <xsl:value-of select="name"/>
</td>
<td>
<xsl:value-of select="capital"/>
</td>
</tr>
    </xsl:for-each>

xml xslt xpath xslt-1.0
1个回答
0
投票
尝试一下

XSLT 1.0

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output indent="yes"/> <xsl:key name="Continent" match="country" use="continent"/> <xsl:template match="countries"> <countries> <xsl:for-each select="//country[generate-id(.) = generate-id(key('Continent', continent)[1])]"> <continent><xsl:value-of select="continent"/></continent> <table border="3" width="100%" align="center"> <tr> <th>Name</th> <th>Capital</th> </tr> <xsl:for-each select="key('Continent', continent)"> <tr> <td > <xsl:value-of select="name"/> </td> <td> <xsl:value-of select="capital"/> </td> </tr> </xsl:for-each> </table> </xsl:for-each> </countries> </xsl:template> </xsl:stylesheet>

XSLT 1.0

参见https://xsltfiddle.liberty-development.net/naZXVEE处的变换>

XSLT 2.0

参见https://xsltfiddle.liberty-development.net/naZXVEE/1处的变换>
© www.soinside.com 2019 - 2024. All rights reserved.