I have XML like this:
<span>1</span>
<span class="x">2</span>
<span class="x y">3</span>
<span class="x">4</span>
<span>5</span>
<span class="x">6</span>
<span>7</span>
<span class="x">8</span>
What I want is to use an XSLT stylesheet to put the contents of all elements whose class attribute contains x into one <x> element. So the output should be like this:
1 <x>234</x> 5 <x>6</x> 7 <x>8</x>
(or, ideally,
1 <x>2<y>3</y>4</x> 5 <x>6</x> 7 <x>8</x>
but that's a problem to tackle when I've solved this one.)
This is the relevant fragment of my XSLT:
<xsl:template match="span[contains(@class,'x') and preceding-sibling::span[1][not(contains(@class,'x'))]]">
<x><xsl:for-each select=". | following-sibling::span[contains(@class,'x')]">
<xsl:value-of select="text()"/>
</xsl:for-each></x>
</xsl:template>
<xsl:template match="span[contains(@class,'x') and preceding-sibling::span[1][contains(@class,'x')]]">
</xsl:template>
<xsl:template match="span">
<xsl:value-of select="text()"/>
</xsl:template>
What this produces is:
1 <x>23468</x> 5 <x>68</x> 7 <x>8</x>
I'm pretty sure I have to use a count in the XPath expression so that it doesn't select all of the following elements with class x, just the contiguous ones. But how can I count the contiguous ones? Or am I doing this the wrong way?
This is tricky, but doable (long read ahead, sorry for that).
The key to "consecutiveness" in terms of XPath axes (which are by definition not consecutive) is to check whether the closest node in the opposite direction that "first fulfills the condition" also is the one that "started" the series at hand:
In your case, a series consists of
<span>nodes that have the stringxin their@class:Note that I concat spaces to avoid false positives.
A
<span>that starts a series (i.e. one that "first fulfills the condition") can be defined as one that has anxin its class and is not directly preceded by another<span>that also has anx:We must check this condition in an
<xsl:if>to avoid that the template generates output for nodes that are in a series (i.e., the template will do actual work only for "starter nodes").Now to the tricky part.
From each of these "starter nodes" we must select all
following-sibling::spannodes that have anxin their class. Also include the currentspanto account for series that only have one element. Okay, easy enough:For each of these we now find out if their closest "starter node" is identical to the one that the template is working on (i.e. that started their series). This means:
they must be part of a series (i.e. they must follow a
spanwith anx)now remove any
spanwhose starter node is not identical to the current series starter. That means we check any preceding-siblingspan(that has anx) which itself is not directly preceded by aspanwith anx:Then we use
generate-id()to check node identity. If the found node is identical to$starter, then the current span is one that belongs to the consecutive series.Putting it all together:
And yes, I know it's not pretty. There is an
<xsl:key>based solution that is more efficient, Dimitre's answer shows it.With your sample input, this output is generated: