I have an XML with records, some records are associated with each other and, as such, I want to group them together in the output.
XML:
<Records>
<Record id="1" group="10" />
<Record id="2" group="20" />
<Record id="3" group="20" />
<Record id="4" group="20" />
</Records>
Currently, I display
<span>1</span><span>2</span><span>3</span><span>4</span>
What I would like to display is (based on the records having the same group)
<span>1</span><span>2-4</span>
I have looked into using preceding-sibling::Record/@group to see if the grouping has changed between iterations of Records but am struggling to figure out how to achieve the 2-4 grouping I require.
Here is what I have so far, interspersed with some comments to illustrate what I am trying to do:
<xsl:for-each select="Records/Record">
<xsl:if test="@group != preceding-sibling::Record/@group">
<!-- obviously here we need 2-4...somehow? -->
<span><xsl:value-of="@id" /></span>
</xsl:if>
</xsl:for-each>
This transformation:
when applied on the provided XML document:
produces the wanted, correct result:
Explanation:
This is positional grouping using a key to define all adjacent Record elements that comprise a group.
This is an efficient (sublinear) algorithm because keys are used. Algorithms using a siblings axis are typically
O(N^2)— quadratical in time complexity and can be too slow if the total number of siblingsNis big.