I have an XML where I want to group some of the items:
<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"/>
If there are several nodes with variant="Release"
in a row, I want to sort just that group using the versionorder
attribute. All other nodes must be output in the order they're in now.
Desired result:
<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"/>
I'm trying to use groups to achieve this:
<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>
I expect the group-adjacent
statement to make 5 groups. Instead it makes a single group and sorts that:
<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"/>
How can I get the desired result?
question from:
https://stackoverflow.com/questions/65919730/group-adjacent-groups-all-items-instead-of-just-the-adjacent-ones 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…