Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 14th, 2008, 03:45 PM
Authorized User
 
Join Date: Jul 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default grouping problem at xslt1.0

Hi all, I have an xml which i need to transform to another xml using the xslt1.0.
<pre>
<?xml version="1.0" encoding="UTF-8"?>
<EXTRACT>
<RECORDS>
    <RECORD>
    <field name='mgr_Code'><![CDATA[A101]]></field>
    <field name='dept_Code'><![CDATA[D1]]></field>
    </RECORD>
    <RECORD>
    <field name='mgr_Code'><![CDATA[A101]]></field>
    <field name='dept_Code'><![CDATA[D2]]></field>
    </RECORD>
    <RECORD>
    <field name='mgr_Code'><![CDATA[A102]]></field>
    <field name='dept_Code'><![CDATA[D3]]></field>
    </RECORD>
    <RECORD>
    <field name='mgr_Code'><![CDATA[A102]]></field>
    <field name='dept_Code'><![CDATA[D4]]></field>
    </RECORD>
</RECORDS>
</EXTRACT>
</pre>

I am using xsl as:

<pre>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="group_by_mgrid" match="RECORD" use="mgr_Code"/>
<xsl:template match="/EXTRACT/RECORDS">
 <myrecords>
   <xsl:for-each select="RECORD[count(. | key('group_by_mgrid',mgr_Code)[1])=1]">
     <row>
       <mgr_Code>
         <xsl:value-of select="field[@name='mgr_Code']"/>
        </mgr_Code>

      <Department>
        <xsl:for-each select="key('group_by_mgrid', mgr_Code)">
     <Dept_Code>
            <xsl:value-of select="."/>
         </Dept_Code>
      </xsl:for-each>
     </Department>
   </row>
  </xsl:for-each>

 </myrecords>
</xsl:template>
</xsl:stylesheet>
</pre>
Which generates as :

<myrecords>
<row>
<mgr_Code>A101</mgr_Code>
<Department/>
</row>
<row>
<mgr_Code>A101</mgr_Code>
<Department/>
</row>
<row>
<mgr_Code>A102</mgr_Code>
<Department/>
</row>
<row>
<mgr_Code>A102</mgr_Code>
<Department/>
</row>
</myrecords>

But I want the output to be like below (Which is grouped by mgr_id)

<myrecords>
<row>
<mgr_Code>A101</mgr_Code>
<Department>
<Dept_Code>D1</Dept_Code>
<Dept_Code>D2</Dept_Code>
</Department>
</row>
<row>
<mgr_Code>A102</mgr_Code>
<Dept_Code>D3</Dept_Code>
<Dept_Code>D4</Dept_Code>
</row>
</myrecords>

Is Anyone have idea how to do this?
Thanks in advance.

 
Old July 14th, 2008, 05:33 PM
Authorized User
 
Join Date: Jul 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It seems to be issue with directly using "mgr_Code"
<xsl:key name="group_by_mgrid" match="RECORD" use="mgr_Code"/>

but after changing to
<xsl:key name="group_by_mgrid" match="RECORD" use="field
[@name='mgr_Code']"/>

Also still the same result. Please let me know to this xslt to dealt with attribute name.



 
Old July 15th, 2008, 03:19 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

This does what you want - there is probably a simpler way but I'm not that good at XSLT 1.0 grouping myself.

A couple of pointers - your xsl:key - was completely empty - there are no RECORD element that are children of the root, therefore match="RECORD/field" would not match anything.

Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>

<xsl:key name="group_by_mgrid" match="/EXTRACT/RECORDS/RECORD/field[@name='mgr_Code']" use="."/>

<xsl:template match="/EXTRACT/RECORDS">
 <myrecords>
   <xsl:for-each select="RECORD/field[generate-id() = generate-id(key('group_by_mgrid',.)[1])]">  
     <row>
       <mgr_Code>
         <xsl:value-of select="."/>
        </mgr_Code>

      <Department>
        <xsl:for-each select="key('group_by_mgrid', .)/../field[@name='dept_Code']">
     <Dept_Code>
            <xsl:value-of select="."/>
         </Dept_Code>
      </xsl:for-each>       
     </Department>  
   </row>  
  </xsl:for-each>   
 
 </myrecords>
</xsl:template>
</xsl:stylesheet>
/- Sam Judson : Wrox Technical Editor -/
 
Old July 15th, 2008, 03:50 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Try changing use="mgr_Code" to use="field[@name='mgr_Code']", and the same in your calls to the key() function

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 15th, 2008, 08:08 AM
Authorized User
 
Join Date: Jul 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot guys. Its working great.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Muenchian Grouping on individual nodes in xslt1.0 sudhish.sikhamani XSLT 9 July 1st, 2008 10:32 AM
NESTED GROUPING USING XSLT1.0 jhansib4u Classic ASP XML 0 November 19th, 2007 09:12 AM
Grouping problem bonekrusher XSLT 3 April 7th, 2007 05:11 PM
A problem with predicate in XSLT1.0:( sureshk XSLT 1 March 8th, 2006 01:13 PM
Grouping problem aware Access 11 August 4th, 2005 11:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.