[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Grouping "span" with same @class within mixed content

Subject: Grouping "span" with same @class within mixed content element
From: Matthieu Ricaud-Dussarget <matthieu.ricaud@xxxxxxxxx>
Date: Mon, 20 Dec 2010 15:45:34 +0100
 Grouping "span" with same @class within mixed content
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="*[@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="*[@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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.