2011-10-04 54 views
0

我有一个XML列表的动物。我想按课程分组,并将它们显示在三排的表格中。我能做到这一点使用XSLT 2.0和-各组分组并排序为多列XSLT

<zoo> 
<animal class="fish">Koi</animal> 
<animal class="fish">Lamprey</animal> 
<animal class="bird">Chicken</animal> 
<animal class="fish">Firefish</animal> 
<animal class="fish">Bluegill</animal> 
<animal class="bird">Eagle</animal> 
<animal class="fish">Eel</animal> 
<animal class="bird">Osprey</animal> 
<animal class="bird">Turkey</animal> 
<animal class="fish">Guppy</animal> 
</zoo> 

的XSLT是:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html" indent="yes" name="html" doctype-system="about:legacy-compat" /> 
<xsl:template match="/"> 
<html><head></head><body> 
     <xsl:for-each-group select="//animal" group-by="@class"> 
     <table> 
      <xsl:for-each-group select="current-group()" group-adjacent="ceiling(position() div 3)"> 
      <tr> 
       <xsl:for-each select="current-group()"> 
        <xsl:apply-templates select="."/> 
       </xsl:for-each> 
      </tr> 
     </xsl:for-each-group> 
     </table> 
     </xsl:for-each-group> 
    </body></html> 
</xsl:template> 

<xsl:template match="animal"> 
    <td><xsl:value-of select="."/></td> 
</xsl:template> 

</xsl:stylesheet> 

输出,例外的是我需要的动物名称排序近乎完美。

我试过使用< xsl:执行排序选择=“当前组()”>像迈克尔凯建议给某人here。但是这导致了堆栈溢出。任何想法如何在我的分组和多列列表中对动物名称进行分类?

回答

0

内这里是一个可行的解决方案(虽然我不知道我理解为什么)。

<xsl:variable name="unsorted" select="current-group()"/> 
<xsl:variable name="sorted"> 
    <xsl:perform-sort select="current-group()"> 
    <xsl:sort select="."/> 
    </xsl:perform-sort> 
</xsl:variable> 
<xsl:for-each-group select="$sorted/animal" group-adjacent="ceiling(position() div 3)"> 
    <tr> 
    <xsl:for-each select="current-group()"> 
    <xsl:apply-templates select="."/> 
    </xsl:for-each> 
    </tr> 
</xsl:for-each-group> 

(当然你不需要任何东西的“无序”变量,它只是有比较)

奇怪的是,如果我使用$在中的for-each未排序-group,它像我期望的那样构建未排序的列表。但是,如果我使用$排序,我不得不使用“$ sorted/animal”,原因我不太明白。

+0

我认为这很好,你做了什么。 –

0

添加排序标签,该<xsl:for-each>

<xsl:for-each select="current-group()"> 
    <xsl:sort data-type="text" select="." order="ascending"/> 
    <xsl:apply-templates select="."/> 
</xsl:for-each> 
+0

这种排序行内的每行三。我需要对整个列表进行排序。 –