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

Re: Positional Grouping Problem

Subject: Re: Positional Grouping Problem
From: omprakash.v@xxxxxxxxxxxxx
Date: Fri, 5 Aug 2005 17:12:44 +0530
xml positional grouping
Hi,
    Your generate-group-elements is recursing over all Zs in the input. And
thats why you are getting the output you are getting.

You need to test for when the next Z has a different value from the current
and make that your stop condition.

Cheers,
prakash




                                                                                                                                       
                      Mukul Gandhi                                                                                                     
                      <gandhi.mukul@gm         To:      xsl-list@xxxxxxxxxxxxxxxxxxxxxx                                                
                      ail.com>                 cc:      (bcc: omprakash.v/Polaris)                                                     
                                               Subject:  Positional Grouping Problem                                              
                      08/05/2005 04:55                                                                                                 
                      PM                                                                                                               
                      Please respond                                                                                                   
                      to xsl-list                                                                                                      
                                                                                                                                       
                                                                                                                                       




I have this XML file -
<?xml version="1.0"?>
<X>
  <Y>
    <Z>HI</Z>
    <Z>HI</Z>
    <Z>HI</Z>
    <Z>YES</Z>
    <Z>HI</Z>
    <Z>HI</Z>
  </Y>
</X>

I desire output -
<?xml version="1.0" encoding="utf-8"?>
<X>
   <Y>
      <group>
         <Z>HI</Z>
         <Z>HI</Z>
         <Z>HI</Z>
      </group>
      <Z>YES</Z>
      <group>
         <Z>HI</Z>
         <Z>HI</Z>
      </group>
   </Y>
</X>

i.e. all consecutive Z's with string HI should be grouped in element
<group>

I have written this stylesheet so far to solve this problem -
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="xml" indent="yes" />

<xsl:template match="/X">
  <X>
   <Y>
     <xsl:apply-templates select="Y" />
   </Y>
  </X>
</xsl:template>

<xsl:template match="Y">
   <xsl:apply-templates select="Z" />
</xsl:template>

<xsl:template match="Z">
   <xsl:choose>
     <xsl:when test=". = 'YES'">
       <xsl:copy-of select="." />
     </xsl:when>
     <xsl:when test="((. = 'HI') and not(preceding-sibling::Z[1] = 'HI'))">
       <group>
         <xsl:copy-of select="." />
         <xsl:call-template name="generate-group-elements">
           <xsl:with-param name="list" select="following-sibling::Z" />
         </xsl:call-template>
       </group>
    </xsl:when>
  </xsl:choose>
</xsl:template>

<xsl:template name="generate-group-elements">
   <xsl:param name="list" />

   <xsl:if test="$list[1]">
     <xsl:copy-of select="$list[1]" />
     <xsl:call-template name="generate-group-elements">
       <xsl:with-param name="list" select="$list[position() > 1]" />
     </xsl:call-template>
   </xsl:if>
</xsl:template>

</xsl:stylesheet>

But I am getting output -
<?xml version="1.0" encoding="utf-8"?>
<X>
   <Y>
      <group>
         <Z>HI</Z>
         <Z>HI</Z>
         <Z>HI</Z>
         <Z>YES</Z>
         <Z>HI</Z>
         <Z>HI</Z>
      </group>
      <Z>YES</Z>
      <group>
         <Z>HI</Z>
         <Z>HI</Z>
      </group>
   </Y>
</X>

Can somebody please find the bug in my stylesheet?

I have tested with Xalan-J 2.6.0, Saxon 6.5.3 and MSXML4 ; and I am
getting same output with all these three XSLT processors. I am hinging
slightly to Xalan and Saxon.

Regards,
Mukul





This e-Mail may contain proprietary and confidential information and is sent for the intended recipient(s) only. 
If by an addressing or transmission error this mail has been misdirected to you, you are requested to delete this mail immediately.
You are also hereby notified that any use, any form of reproduction, dissemination, copying, disclosure, modification,
distribution and/or publication of this e-mail message, contents or its attachment other than by its intended recipient/s is strictly prohibited.

Visit Us at http://www.polaris.co.in

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.