首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对多列进行分组和排序XSLT

对多列进行分组和排序XSLT
EN

Stack Overflow用户
提问于 2011-10-05 02:04:34
回答 2查看 1.1K关注 0票数 0

我有一个XML格式的动物列表。我想按类对它们进行分组,并以三行为一组的表格形式显示它们。我可以使用XSLT2.0和for-each-group来实现这一点

代码语言:javascript
复制
<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是:

代码语言:javascript
复制
<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>

除了我需要对动物名称进行排序之外,输出几乎是完美的。

我试着像Michael Kay对某个here建议的那样使用。但这导致了堆栈溢出。您知道如何对分组和多列列表中的动物名称进行排序吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-05 10:12:52

这里有一个有效的解决方案(尽管我不确定我明白为什么)。

代码语言:javascript
复制
<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>

(当然,任何事情都不需要"unsorted“变量。它只是为了进行比较)

奇怪的是,如果我在for-each-group中使用$unsorted,它会按照我的预期构建未排序列表。然而,如果我使用$sorted,我不得不使用"$sorted/animal“,原因我不太明白。

票数 0
EN

Stack Overflow用户

发布于 2011-10-05 02:51:02

<xsl:for-each>中添加排序标记

代码语言:javascript
复制
<xsl:for-each select="current-group()">
    <xsl:sort data-type="text" select="." order="ascending"/>
    <xsl:apply-templates select="."/>
</xsl:for-each>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7652146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档