This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Grouping Nodes ?


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]