xsl-list
[Top] [All Lists]

[xsl] Grouping "span" with same @class within mixed content element

2010-12-20 08:45:46
Hi all,

My question is about tranforming xhtml to xhtml with XSLT2.

In my input XHTML document I have paragraphes with such a structure :

<p>
    text1
<span class="foo">text2</span>
    text3
<span class="bar">text4</span>
<span class="bar">text5</span>
<span class="foo>text6</span>
<span class="foo">text7</span>
<span class="foo">text8</span>
<span>text9</span>
<span class="foo">text10</span>
</p>

(Every span could also contains other spans, but to keep simple i didn't add this complexity to the example. I have indented every nodes so it's clear, but offcourse it should be inline)

For this sample <p> the desired xhtml output is :

<p>
    text1
<span class="foo">text2</span>
    text3
<span class="bar">text4 text5</span>
<span class="foo>text6 text7 text8</span>
<span>text9</span>
<span class="foo">text10</span>
</p>

(please don't care about the spaces i added between "text{n}" and "text{n+1}", this has to do with indentation, but that's not the problem here)

To do such a grouping in XSLT2, I thought about using group-adjacent grouping. To use this for-each-group, i guess my context node should be the <p> element itself.
So this is part of XSLT i try to use :

<xsl:template match="p" >
<xsl:copy>
<xsl:for-each-group select="*[(_at_)class]" group-adjacent="@class">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<span class="{current-group()[1]/@class}">
<xsl:apply-templates select="current-group()/node()" />
</span>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()/node()" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>

It doesn't work because all selected nodes in the for-each-group don't have a @class attribute, which is an error : => XTTE1100: An empty sequence is not allowed as the @group-adjacent attribute of xsl:for-each-group

So I tried <xsl:for-each-group select="*[(_at_)class]" group-adjacent="@class"> which works (no XSLT error) but which doesn't do the trick : text elements under <p> are not matched don't appear in output, that's the same with span without @class
I get :

<p>
<span class="foo">text2</span>
<span class="bar">text4 text5</span>
<span class="foo>text6 text7 text8</span>
<span class="foo">text10</span>
</p>

Is there a solution to select every nodes (text include) in the grouping loop and to "group-adjacent" on the @class ?
Or should I use something quite different to do this grouping ?

Thanks in advance for your lightening my view,

Regards,

Matt


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--