2015-07-21 86 views
0

结合两个回路我有这样的结构在我的xml:XSL在同一水平

<zoo> 
    <species name="bird" /> 
    <animal name="crane" /> 
    <animal name="duck" /> 
    <species name="fish" /> 
    <animal name="dolphin" /> 
    <animal name="goldfish" /> 
</zoo> 

,我想变成像这样:

<table> 
    <tr><td> <b>bird</b> </td></tr> 
    <tr><td> crane </td></tr> 
    <tr><td> duck </td></tr> 
</table> 

<table> 
    <tr><td> <b>fish</b> </td></tr> 
    <tr><td> dolphin </td></tr> 
    <tr><td> goldfish </td></tr> 
</table> 

我怎样才能使这项工作?我尝试使用嵌套for:each'ES,但显然不工作,因为节点没有嵌套。

+1

请选择XSLT 1.0或2.0,而不是两者。对你的情况有很大的影响。 –

+0

我删除了XSLT 1.0标记 – DUBWiSE

回答

1

假设XSLT 2.0处理程序等撒克逊9或XmlPrime可以使用for-each-group group-starting-with="species"

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> 

    <xsl:template match="/"> 
     <htmt> 
     <head> 
      <title>group-starting-with</title> 
     </head> 
     <body> 
      <xsl:apply-templates/> 
     </body>   
     </html> 
    </xsl:template> 

    <xsl:template match="zoo"> 
     <xsl:for-each-group select="*" group-starting-with="species"> 
      <table> 
       <xsl:apply-templates select="current-group()"/> 
      </table> 
     </xsl:for-each-group> 
    </xsl:template> 

    <xsl:template match="species"> 
     <tr> 
      <th> 
      <xsl:value-of select="@name"/> 
      </th> 
     </tr> 
    </xsl:template> 

    <xsl:template match="animal"> 
     <tr> 
      <td><xsl:value-of select="@name"/></td> 
     </tr> 
    </xsl:template> 
</xsl:transform> 

在线在http://xsltransform.net/nc4NzRc/1