xsl-list
[Top] [All Lists]

Re: Positional Grouping Problem

2005-08-05 04:56:02
Thanks George :)

Regards,
Mukul

On 8/5/05, George Cristian Bina <george(_at_)sync(_dot_)ro> wrote:
Hi Mukul,

You need to change the generate-group-elements template to stop copying
when it encounters something different of HI, you can do this like below
for instance:

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

    <xsl:if test="$list[1]='HI'">
      <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>

Alternatively you can place inside the list parameter only the desired Z
elements.

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com


Mukul Gandhi wrote:
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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--