Wednesday, 7 March 2007

Collapsing on clusters

I’ve seen use of preceding-sibling to collapse on unique attributes, but how about collapsing clusters on unique attributes? For example:
a,a,a,b,a,b,b,c should collapse to a,b,a,b,c

Here’s some XSLT I came up with to detect which position a node is in it’s cluster, and what cluster number it is. NODE repesents the nodes being collapsed, and VALUE/@value a child node/attribute that is being collapsed upon.

Here we look for the number of nodes that are not preceded by a node having the same value - this will be the number of clusters.


<xsl:variable name="cluster-no" select="count(preceding-sibling::NODE[
  not(VALUE/@value = preceding-sibling::NODE[1]/VALUE/@value) ]
 |current()[not(VALUE/@value = preceding-sibling::NODE[1]/VALUE/@value)]
)"/>


He we count the number of nodes that precede the current node and have the same value, but there is no intervening node with a different value, this will be the position in the current cluster.


<xsl:variable name="position-in-cluster" select="count(preceding-sibling::NODE[VALUE/@value = current()/VALUE/@value
and not( VALUE/@value!=following-sibling::NODE[ count(following-sibling::NODE[ generate-id()=generate-id(current())]) &gt; 0 ]/VALUE/@value)])"/>

No comments:

Post a comment