Re: [xsl] Joining sibling elements

Subject: Re: [xsl] Joining sibling elements
From: "Joris Gillis" <roac@xxxxxxxxxx>
Date: Wed, 10 Aug 2005 22:06:52 +0200
Tempore 19:49:30, die 08/10/2005 AD, hinc in xsl-list@xxxxxxxxxxxxxxxxxxxxxx scripsit Marcin MiEkowski <milek_pl@xxxxx>:

<b>this</b><b> </b><b>is</b><b> </b><b>bold<text</b>

which should be transformed this:

<b>this is bold text</b>

Here's a stylesheet that will handle this simple scenario:


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>

<xsl:template match="*[name(preceding-sibling::node()[1])=name()]"/>

<xsl:template match="*">
	<xsl:copy>
		<xsl:apply-templates/>
		<xsl:call-template name="mergeSiblings"/>
	</xsl:copy>
</xsl:template>

<xsl:template name="mergeSiblings">
<xsl:for-each select="following-sibling::node()[1][name()=name(current())]">
		<xsl:apply-templates/>
		<xsl:call-template name="mergeSiblings"/>
</xsl:for-each>	
</xsl:template>

</xsl:stylesheet>

The more complex case can be solved with this algorithm in multiple passes.

regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
Vincit omnia simplicitas
Keep it simple

Current Thread