2015-07-21 83 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