Group-adjacent 将所有项目分组,而不仅仅是相邻的项目

Group-adjacent groups all items instead of just the adjacent ones

我有一个 XML,我想在其中对一些项目进行分组:

    <Topic id="2807" variant="noVariant" versionorder="noVersion"/>
    <Topic id="2808" variant="Release" versionorder="5"/>
    <Topic id="2809" variant="noVariant" versionorder="noVersion"/>
    <Topic id="2813" variant="noVariant" versionorder="noVersion"/>
    <Topic id="2814" variant="Release" versionorder="9"/>
    <Topic id="2815" variant="Release" versionorder="14"/>
    <Topic id="2816" variant="Release" versionorder="12"/>
    <Topic id="2817" variant="noVariant" versionorder="noVersion"/>
    <Topic id="2820" variant="Release" versionorder="5"/>
    <Topic id="2821" variant="noVariant" versionorder="noVersion"/>
    <Topic id="2824" variant="Release" versionorder="5"/>
    <Topic id="2825" variant="noVariant" versionorder="noVersion"/>
    <Topic id="2855" variant="noVariant" versionorder="noVersion"/>
    <Topic id="2875" variant="Release" versionorder="12"/>
    <Topic id="2881" variant="Release" versionorder="17"/>
    <Topic id="2883" variant="Release" versionorder="19"/>  
    <Topic id="2879" variant="Release" versionorder="15"/>
    <Topic id="2885" variant="Release" versionorder="21"/>
    <Topic id="2887" variant="noVariant" versionorder="noVersion"/>

如果有多个节点连续 variant="Release",我想使用 versionorder 属性仅对该组进行排序。所有其他节点必须按照它们现在的顺序输出。

想要的结果:

    <Topic id="2807" variant="noVariant" versionorder="noVersion"/>
    <Topic id="2808" variant="Release" versionorder="5"/>
    <Topic id="2809" variant="noVariant" versionorder="noVersion"/>
    <Topic id="2813" variant="noVariant" versionorder="noVersion"/>
    <Topic id="2814" variant="Release" versionorder="9"/>
    <Topic id="2816" variant="Release" versionorder="12"/>
    <Topic id="2815" variant="Release" versionorder="14"/>
    <Topic id="2817" variant="noVariant" versionorder="noVersion"/>
    <Topic id="2820" variant="Release" versionorder="5"/>
    <Topic id="2821" variant="noVariant" versionorder="noVersion"/>
    <Topic id="2824" variant="Release" versionorder="5"/>
    <Topic id="2825" variant="noVariant" versionorder="noVersion"/>
    <Topic id="2855" variant="noVariant" versionorder="noVersion"/>
    <Topic id="2875" variant="Release" versionorder="12"/>
    <Topic id="2879" variant="Release" versionorder="15"/>
    <Topic id="2881" variant="Release" versionorder="17"/>
    <Topic id="2883" variant="Release" versionorder="19"/>
    <Topic id="2885" variant="Release" versionorder="21"/>
    <Topic id="2887" variant="noVariant" versionorder="noVersion"/>

我正在尝试使用群组来实现此目的:

<xsl:template match="Objects">
    <xsl:for-each-group select="Topic[not(@versionorder='noVersion')]" group-adjacent="@variant">
        <xsl:for-each select="current-group()">
        <xsl:sort select="number(@versionorder)"/>
            <xsl:copy>
            <xsl:apply-templates select="@* | node()" xml:space="preserve"/>
            </xsl:copy>
        </xsl:for-each>
    </xsl:for-each-group>
    
    <xsl:for-each select="Topic[@versionorder='noVersion']">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" xml:space="preserve"/>
        </xsl:copy>
    </xsl:for-each>
</xsl:template>

我希望 group-adjacent 声明为 make 5 groups。相反,它创建一个单独的组并排序:

<Topic id="2808" variant="Release" versionorder="5"/>
<Topic id="2820" variant="Release" versionorder="5"/>
<Topic id="2824" variant="Release" versionorder="5"/>
<Topic id="2814" variant="Release" versionorder="9"/>
<Topic id="2816" variant="Release" versionorder="12"/>
<Topic id="2875" variant="Release" versionorder="12"/>
<Topic id="2815" variant="Release" versionorder="14"/>
<Topic id="2815" variant="Release" versionorder="14"/>
<Topic id="2879" variant="Release" versionorder="15"/>
<Topic id="2881" variant="Release" versionorder="17"/>
<Topic id="2883" variant="Release" versionorder="19"/>
<Topic id="2885" variant="Release" versionorder="21"/>

怎样才能得到想要的结果?

我想你想要

  <xsl:template match="Objects">
      <xsl:for-each-group select="Topic" group-adjacent="@variant = 'Release'">
          <xsl:choose>
              <xsl:when test="current-grouping-key()">
                  <xsl:apply-templates select="current-group()">
                      <xsl:sort select="xs:decimal(@versionorder)"/>
                  </xsl:apply-templates>
              </xsl:when>
              <xsl:otherwise>
                  <xsl:apply-templates select="current-group()"/>
              </xsl:otherwise>
          </xsl:choose>
      </xsl:for-each-group>
  </xsl:template>

这假设恒等变换模板设置为复制节点的基本模板。