Re: [xsl] Join elements by name, with spaces in between

Subject: Re: [xsl] Join elements by name, with spaces in between
From: Yves Forkl <Y.Forkl@xxxxxx>
Date: Mon, 12 Feb 2007 16:54:50 +0100
Michael Kay schrieb:
Try group-adjacent using a grouping key of

group-adjacent="if (. instance of element()) then name(.) else
name(following-sibling::*[1])"

This groups text nodes with the following element, which may not be what you
want. If you only want to retain text nodes if they fall between to element
nodes that are in the same group, you could try to expand the conditional
above.

Thank you very much for this expression that would have taken me weeks to come up with...


You are right that I want to include spaces in an element only if the space was formerly between instances of the same element, so I have expanded the conditional expression as follows (sorry for the mangled indentation):

<xsl:template match="e">
<xsl:copy>
<xsl:for-each-group select="node()"
group-adjacent="if (. instance of element()) then name(.) else
(if (name(preceding-sibling::*[1]) eq name(following-sibling::*[1]))
then name(preceding-sibling::*[1]) else 'space_outside')">
<xsl:element name="name(current-group()[1])">
<xsl:apply-templates select="current-group()/text()"/>
</xsl:element>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>


So far I haven't been able to test if it works because I don't know how to dynamically set the "new" element's name in xsl:element from the element which is the "leader" of the current group. Am I going the wrong way about joining instances of the same element type?

To recall, I want to transform this

<e><x>x</x> <a>a1</a> <a>a2</a> <b>b</b> <c>c1</c> <c>c2</c> <a>a3</a></e>

into this:

<e><x>x</x> <a>a1 a2</a> <b>b</b> <c>c1 c2</c> <a>a3</a></e>

Yves

Current Thread