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

RE: Enclosing following-sibling nodes before the next

Subject: RE: Enclosing following-sibling nodes before the next element of the same type
From: <Jarno.Elovirta@xxxxxxxxx>
Date: Fri, 12 Nov 2004 08:05:11 +0200
following sibling 1
Hi,

> How can I transform the following XML:
>
> <parent><elem/>text<br/><elem/>word<hr/>word<elem/></parent>
>
> to
>
> <parent>
> <elem>text<br/></elem>
> <elem>word<hr/>word</elem>
> </parent>
>
> I'd like <elem> to enclose all its following-siblings up to
> either the the next
> <elem> or the end tag of its parent element.
> How can I do this in XSLT ? Anyone know how can I select these nodes ?

This is a FAQ, a grouping problem. See Jeni's pages
<http://jenitennison.com/xslt/grouping> for more info.

  <xsl:template match="parent">
    <xsl:copy>
      <xsl:apply-templates select="elem[1]"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="elem">
    <xsl:if test="following-sibling::elem[1]">
      <xsl:copy>
        <xsl:apply-templates select="following-sibling::node()[1]"
mode="group"/>
      </xsl:copy>
      <xsl:apply-templates select="following-sibling::elem[1]"/>
    </xsl:if>
  </xsl:template>
  <xsl:template match="node()" mode="group">
    <xsl:copy>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
    <xsl:apply-templates select="following-sibling::node()[1]" mode="group"/>
  </xsl:template>
  <xsl:template match="elem" mode="group"/>
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

Cheers,

Jarno

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.