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: Tue, 13 Feb 2007 10:50:43 +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.

Matching the whitespace nodes in between two element instances seems to be rather difficult, so now I try to group my text nodes with the *preceding* element (grouping with the following element fails, obviously because sometimes there is no following element node) and move the spaces out again later.


I am using these templates (moving the spaces out is to be done later):

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

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>


This works, short of an important detail - it eats up all whitespace, but I don't know why :-(.


So given my input of

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

I get

<e><x>x</x><a>a1a2</a><b>b</b><c>c1c2</c><a>a3</a></e>

instead of

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


What might be the reason for this?


Yves

Current Thread