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

Re: Join elements by name, with spaces in between

Subject: Re: Join elements by name, with spaces in between
From: Yves Forkl <Y.Forkl@xxxxxx>
Date: Tue, 13 Feb 2007 10:50:43 +0100
Re:  Join elements by name
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

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-2007 All Rights Reserved.