0

我有一个 XML 格式的动物列表。我想按班级对它们进行分组,并将它们显示在三行的表格中。我可以使用 XSLT 2.0 和 for-each-group 来完成此操作

<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:perform-sort select="current-group()"> 就像迈克尔凯在这里向某人建议的那样。但这导致了堆栈溢出。任何想法如何在我的分组和多列列表中对动物的名称进行排序?

4

2 回答 2

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>
于 2011-10-04T18:51:02.270 回答
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>

(当然你不需要任何“未排序”变量。它只是为了比较)

The odd thing is that if I use $unsorted in the the for-each-group, It builds the unsorted list like I'd expect. However if I use $sorted I have to use "$sorted/animal" for reasons I don't quite understand.

于 2011-10-05T02:12:51.810 回答