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

Grouping Nodes ?

Subject: Grouping Nodes ?
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Tue, 12 Jun 2001 00:36:56 -0700 (PDT)
grouping nodes in xsl
Ciaran Byrne wrote:

> I'm attempting to transform the following
> 
> <go/><postfield id="A"/><refresh/><postfield id="B"/>
> 
> into....
> 
> <go><postfield id="A"/></go><refresh><postfield id="B"/></refresh>
> 
> i.e. Wrap the 'postfield' element in a 'go' until a 
> sibling is encountered which isn't a 'postfield'.
> In this case it's 'refresh'. The same rules apply for 'refresh'.
> 
> I won't include my code since it's useless ;) but any help is appreicated.

Hi Ciaran,

Here's the solution:

xml source:
----------
<top>
    <go />
    <postfield id="A" />
    <postfield id="A" />
    <postfield id="A" />
    <refresh />
    <postfield id="B" />
    <postfield id="B" />
</top>

stylesheet:
----------
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="*[name()='go' or name()='refresh']">
      <xsl:choose>
         <xsl:when test="following-sibling::*[1][name()='postfield']">
           <xsl:variable name="firstNonWrappable"
                         select="following-sibling::*
                                         [name() != 'postfield'][1]"/>
           <xsl:variable name="firstNonWrappablePosition">
             <xsl:choose>
               <xsl:when test="$firstNonWrappable">
                 <xsl:value-of
                    select="count($firstNonWrappable/preceding-sibling::*)"/>
               </xsl:when>
               <xsl:otherwise>
                 <xsl:value-of select="count(../*) + 1"/>
               </xsl:otherwise>
             </xsl:choose>
           </xsl:variable>
            <xsl:copy>
               <xsl:apply-templates select="following-sibling::*
                                            [count(preceding-sibling::*)
                                            &lt;
                                             $firstNonWrappablePosition
                                            ]"
                                     mode="wrapped"/>
            </xsl:copy>
         </xsl:when>
         <xsl:otherwise>
           <xsl:copy-of select="."/>
         </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
    
    <xsl:template match="postfield" mode="wrapped">
      <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>



Result:
------
<go>
<postfield id="A" />
<postfield id="A" />
<postfield id="A" />
</go>
<refresh>
<postfield id="B" />
<postfield id="B" />
</refresh>

Hope this helped.

Cheers,
Dimitre Novatchev.



__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread
  • Grouping Nodes ?
    • Ciaran Byrne - Mon, 11 Jun 2001 11:26:09 -0400 (EDT)
      • <Possible follow-ups>
      • Dimitre Novatchev - Tue, 12 Jun 2001 03:34:22 -0400 (EDT) <=

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.