Re: [xsl] xml to xml issue in XSLT2.0

Subject: Re: [xsl] xml to xml issue in XSLT2.0
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 19 Nov 2007 13:34:10 -0500
At 2007-11-19 10:21 -0800, chun ji wrote:
I was trying to convert this xml file into a new one
in XSLT 2.0, but failed. The main issue is I have to
deal with one group-by with two level of elements: tab
and sub. I could not find any way to work around with
it. Help if you can.

You don't say what aspect of grouping you are having a problem with.


Below is a solution ... it is straightforward when grouping with XSLT 2 to act on the current group.

Note that the current element within a <xsl:for-each-group> is the first member of the group.

I hope this helps.

. . . . . . . . . . Ken


T:\ftemp>type chun.xml <?xml version="1.0" encoding="UTF-8"?> <perm_testing> <user name="SFO"> <tab name="YourSource"> <sub name="Client"> <checkpoint name="View" value="no"/> </sub> </tab> <tab name="Requisition"> <sub name="General Info"> <checkpoint name="View" value="yes"/> </sub> </tab> <tab name="Requisition"> <sub name="General Info"> <checkpoint name="Create" value="no"/> </sub> </tab> <tab name="Requisition"> <sub name="General Info"> <checkpoint name="Edit" value="no"/> </sub> </tab> <tab name="Requisition"> <sub name="Discussion"> <checkpoint name="View" value="no"/> </sub> </tab> <tab name="Requisition"> <sub name="Discussion"> <checkpoint name="Create" value="yes"/> </sub> </tab> <tab name="Requisition"> <sub name="Discussion"> <checkpoint name="Edit" value="no"/> </sub> </tab> <tab name="Requisition"> <sub name="Resume/Quote"> <checkpoint name="View" value="no"/> </sub> </tab> </user> <user name="ABC"> <tab name="YourSource"> <sub name="Client"> <checkpoint name="View" value="yes"/> </sub> </tab> <tab name="Requisition"> <sub name="General Info"> <checkpoint name="View" value="yes"/> </sub> </tab> <tab name="Requisition"> <sub name="General Info"> <checkpoint name="Create" value="no"/> </sub> </tab> </user> </perm_testing>

T:\ftemp>type chun.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="2.0">

<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="user">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:for-each-group select="tab" group-by="@name">
      <tab name="{@name}">
        <xsl:for-each-group select="current-group()/sub" group-by="@name">
          <sub name="{@name}">
            <xsl:apply-templates select="current-group()/checkpoint"/>
          </sub>
        </xsl:for-each-group>
      </tab>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

<xsl:template match="@*|node()"><!--identity for all other nodes-->
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>


</xsl:stylesheet> T:\ftemp>call xslt2 chun.xml chun.xsl chun.out

T:\ftemp>type chun.out
<?xml version="1.0" encoding="UTF-8"?>
<perm_testing>
   <user name="SFO">
      <tab name="YourSource">
         <sub name="Client">
            <checkpoint name="View" value="no"/>
         </sub>
      </tab>
      <tab name="Requisition">
         <sub name="General Info">
            <checkpoint name="View" value="yes"/>
            <checkpoint name="Create" value="no"/>
            <checkpoint name="Edit" value="no"/>
         </sub>
         <sub name="Discussion">
            <checkpoint name="View" value="no"/>
            <checkpoint name="Create" value="yes"/>
            <checkpoint name="Edit" value="no"/>
         </sub>
         <sub name="Resume/Quote">
            <checkpoint name="View" value="no"/>
         </sub>
      </tab>
   </user>
   <user name="ABC">
      <tab name="YourSource">
         <sub name="Client">
            <checkpoint name="View" value="yes"/>
         </sub>
      </tab>
      <tab name="Requisition">
         <sub name="General Info">
            <checkpoint name="View" value="yes"/>
            <checkpoint name="Create" value="no"/>
         </sub>
      </tab>
   </user>
</perm_testing>
T:\ftemp>rem Done!



--
Comprehensive in-depth XSLT2/XSL-FO1.1 classes: Austin TX,Jan-2008
World-wide corporate, govt. & user group XML, XSL and UBL training
RSS feeds:     publicly-available developer resources and training
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread