XSLT:使用for-each-group分成组

时间:2018-11-25 17:11:35

标签: xml xslt xslt-2.0

我想使用for-each-group按字符的加速度(加速度)对字符进行分组并对其进行排序。这样的格式将如下所示:

<results>
  <result accel =" lowest-value">
     <name>Character 1</name>
     <name>Character 2</name>
     ...
  </result>
  ...
  <result accel="highest-value">
     <name>Character 7</name>
     <name>Character 8</name>
      ...
  </result>
</results>

我已经计算出一个平均值,然后在名称中添加了一个属性以对其进行分组,但是我仍然坚持如何在其上使用for-each-group。我是使用XSLT的新手,但下面是我的尝试:

<xsl:template match ="/">
    <xsl:variable name="path" select="mariokart/characters/character"/>
    <xsl:variable name="accel" select="mariokart/characters/character/accel"/>
    <xsl:variable name="name" select="mariokart/characters/character/name"/>
    <xsl:variable name="averageaccel" select="sum($accel) div count($path)"/>

    <xsl:for-each select="$name">
        <xsl:copy>
            <xsl:attribute name="accelgroup">
                <xsl:choose>
                    <xsl:when test="(../accel&gt;=$averageaccel)">
                        <xsl:text>highest-value</xsl:text>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:text>lowest-value</xsl:text>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:attribute>
        </xsl:copy>
    </xsl:for-each>

    <results>     
        <xsl:for-each-group select="$name" group-by="@accelgroup">
            <result accel="{current-grouping-key()}">
                <xsl:for-each select="current-group()">
                    <name><xsl:copy-of select="."/></name>
                </xsl:for-each>
            </result>
        </xsl:for-each-group>
    </results>
</xsl:template>

xml样本:

<mariokart>
<characters>
    <character>
        <name>Baby Peach</name>
        <speed>
            <land>2.25</land>
            <anti-g>2</anti-g>
            <water>2.5</water>
            <air>2.75</air>
        </speed>
        <accel>4</accel>
        <weight>2</weight>
        <handling>
            <land>5</land>
            <anti-g>5</anti-g>
            <water>4.5</water>
            <air>5</air>
        </handling>
        <traction>4.25</traction>
        <M-turbo>4</M-turbo>
    </character>
    <character>
        <name>Baby Rosalina</name>
        <speed>
            <land>2.25</land>
            <anti-g>2</anti-g>
            <water>2.5</water>
            <air>2.75</air>
        </speed>
        <accel>4.25</accel>
        <weight>2</weight>
        <handling>
            <land>4.75</land>
            <anti-g>4.75</anti-g>
            <water>4.25</water>
            <air>4.75</air>
        </handling>
        <traction>3.75</traction>
        <M-turbo>4</M-turbo>
    </character>
    <character>
        <name>Baby Mario</name>
        <speed>
            <land>2.5</land>
            <anti-g>2.25</anti-g>
            <water>2.75</water>
            <air>3</air>
        </speed>
        <accel>4.25</accel>
        <weight>2.25</weight>
        <handling>
            <land>4.5</land>
            <anti-g>4.5</anti-g>
            <water>4</water>
            <air>4.5</air>
        </handling>
        <traction>4</traction>
        <M-turbo>3.75</M-turbo>
    </character>
</characters>
</mariokart>

1 个答案:

答案 0 :(得分:0)

  

我想...按照字符的加速度(加速度)对它们进行分组,然后   对组进行排序。

您不能简单地通过以下方式实现此目标:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/mariokart">
    <results>
        <xsl:for-each-group select="characters/character" group-by="accel">
            <xsl:sort select="current-grouping-key()" data-type="number" order="ascending"/>
            <result accel="{current-grouping-key()}">
                <xsl:copy-of select="current-group()/name"/>
            </result>
        </xsl:for-each-group>
    </results>
</xsl:template>

</xsl:stylesheet>

演示http://xsltransform.hikmatu.com/bdxtpE