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?
I. XSLT Solutions:
This transformation:
when applied on the provided XML document (wrapped into a single top element
html
to be made well-formed):produces the wanted, correct result:
Then the "ideally" addition:
Just add to the above solution this template:
When applying the so modified solution to the same XML document, again the (new) wanted result is produced:
II. XSLT 2.0 Solution:
When this XSLT 2.0 transformation is applied on the same XML document (above), the wanted "ideal" result is produced:
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 stringx
in 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 anx
in 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::span
nodes that have anx
in their class. Also include the currentspan
to 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
span
with anx
)now remove any
span
whose 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 aspan
with 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:
Thanks for the solutions. In the meantime I've managed to put together something using a completely different tactic. I'm just learning XSLT for this project and the most helpful thing I've read is that XSLT is like functional programming. So I wrote something using recursion, after being pointed in the right direction by this:
I have no idea whether this is more or less efficient than doing it with
generate-id()
or keys, but I certainly learned something from your solutions!