0

XML で動物のリストを作成しています。それらをクラスごとにグループ化し、3 行のテーブルに表示したいと考えています。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()"> を使用してみました Michael Kay がここで誰かに提案したように。しかし、これによりスタックオーバーフローが発生しました。グループ化された複数列のリストで動物の名前を並べ替える方法はありますか?

4

2 に答える 2

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 で $unsorted を使用すると、期待どおりにソートされていないリストが作成されます。ただし、$sorted を使用する場合、よくわからない理由で "$sorted/animal" を使用する必要があります。

于 2011-10-05T02:12:51.810 に答える
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 に答える