If I understood you correctly, the "class" you mention in this case is the color
attribute.
You said that it works when all the items have the same color. So I assume that you were doing something like this, on each node (calculate the difference between the current node's value and the preceding one):
@value - preceding-sibling::item[1]/@value
If you add a predicate to restrict that node-set to the item
elements that have the same color
the first preceding-sibling
will now refer to the previous item
with the same @color
:
@value - preceding-sibling::item[@color = current()/@color][1]/@value"
You can then achieve your expected result XML fragment using any XSLT stylesheet (can be XSLT 1.0):
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="item">
<xsl:variable name="change" select="@value - preceding-sibling::item[@color = current()/@color][1]/@value"/>
<xsl:if test="string($change)">
<item_value_change color="{@color}" change="{$change}"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
The test in the <xsl:if>
requires the string()
otherwise a case of 0
would not be printed.