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 May 4th, 2012, 01:42 AM
Authorized User
 
Join Date: May 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need some help in Grouping logic using xsl:key

Hi ,

I need some help with grouping the nodes. Below are input xml and expected output.

input :

<response>
<GetTestWSResponse>
<CardNumber>12345</CardNumber>
<ProgramInfoList>
<ProgramInfo>
<PartnerInformationGrp>
<PartnerId>H</PartnerId>
<ProgId>H00002</ProgId>
</PartnerInformationGrp>
<AmtInformationGrp>
<HighAmt>19999.00</HighAmt>
<LowAmt>0.00</LowAmt>
</AmtInformationGrp>
</ProgramInfo>
<ProgramInfo>
<PartnerInformationGrp>
<PartnerId>H</PartnerId>
<ProgId>H00001</ProgId>
</PartnerInformationGrp>
<AmtInformationGrp>
<HighAmt>24.00</HighAmt>
<LowAmt>2.00</LowAmt>
</AmtInformationGrp>
</ProgramInfo>
<ProgramInfo>
<PartnerInformationGrp>
<PartnerId>H</PartnerId>
<ProgId>H00002</ProgId>
</PartnerInformationGrp>
<AmtInformationGrp>
<HighAmt>19.00</HighAmt>
<LowAmt>12.00</LowAmt>
</AmtInformationGrp>
</ProgramInfo>
<ProgramInfo>
<PartnerInformationGrp>
<PartnerId>H</PartnerId>
<ProgId>H00001</ProgId>
</PartnerInformationGrp>
<AmtInformationGrp>
<HighAmt>1744.00</HighAmt>
<LowAmt>99.00</LowAmt>
</AmtInformationGrp>
</ProgramInfo>

</ProgramInfoList>
</GetTestWSResponse>
</
response>


output :

<response>
<GetTestWSResponse>
<CardNumber>12345</CardNumber>
<ProgramInfoList>
<ProgramInfo>
<PartnerInformationGrp>
<PartnerId>H</PartnerId>
<ProgId>H00002</ProgId>
</PartnerInformationGrp>
<AmtInformationGrp>
<HighAmt>19999.00</HighAmt>
<LowAmt>0.00</LowAmt>
</AmtInformationGrp>
<AmtInformationGrp>
<HighAmt>19.00</HighAmt>
<LowAmt>12.00</LowAmt>
</AmtInformationGrp>
</ProgramInfo>
<ProgramInfo>
<PartnerInformationGrp>
<PartnerId>H</PartnerId>
<ProgId>H00001</ProgId>
</PartnerInformationGrp>
<AmtInformationGrp>
<HighAmt>24.00</HighAmt>
<LowAmt>2.00</LowAmt>
</AmtInformationGrp>
<AmtInformationGrp>
<HighAmt>1744.00</HighAmt>
<LowAmt>99.00</LowAmt>
</AmtInformationGrp>
</ProgramInfo>
</ProgramInfoList>
</GetTestWSResponse>
</
response>

I tried using xsl:key to find the unique Progid ,but i am not able to figure out how i can get the sibling node for <ProgramInfo>

<xsl:variable name="unique-ProgId" select="//ProgramInfo/PartnerInformationGrp[generate-id(.)=generate-id(key('progId-ref',ProgId))]"/>
<!--<xsl:variable name="unique-ProgId" select="//ProgramInfo/PartnerInformationGrp[key('progId-ref',ProgId)]"/>-->
<xsl:for-each select="$unique-ProgId">
<result>

<xsl:copy-of select="following::ProgramInfo[PartnerInformationGrp/ProgId= "/>
</result>
</xsl:for-each>

Regards.
Arun
 
Old May 5th, 2012, 06:06 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is how I would do it with XSLT 1.0:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>
  
  <xsl:key name="by-ProgId" match="ProgramInfo" use="PartnerInformationGrp/ProgId"/>
  
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="ProgramInfoList">
    <xsl:copy>
      <xsl:apply-templates select="ProgramInfo[generate-id() = generate-id(key('by-ProgId', PartnerInformationGrp/ProgId)[1])]" mode="group"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="ProgramInfo" mode="group">
    <xsl:copy>
      <xsl:apply-templates select="PartnerInformationGrp | key('by-ProgId', PartnerInformationGrp/ProgId)/AmtInformationGrp"/>
    </xsl:copy>
  </xsl:template>
  
</xsl:stylesheet>
With that stylesheet the input
Code:
<response>
  <GetTestWSResponse>
    <CardNumber>12345</CardNumber>
    <ProgramInfoList>
      <ProgramInfo>
        <PartnerInformationGrp>
          <PartnerId>H</PartnerId>
          <ProgId>H00002</ProgId>
        </PartnerInformationGrp>
        <AmtInformationGrp>
          <HighAmt>19999.00</HighAmt>
          <LowAmt>0.00</LowAmt>
        </AmtInformationGrp>
      </ProgramInfo>
      <ProgramInfo>
        <PartnerInformationGrp>
          <PartnerId>H</PartnerId>
          <ProgId>H00001</ProgId>
        </PartnerInformationGrp>
        <AmtInformationGrp>
          <HighAmt>24.00</HighAmt>
          <LowAmt>2.00</LowAmt>
        </AmtInformationGrp>
      </ProgramInfo>
      <ProgramInfo>
        <PartnerInformationGrp>
          <PartnerId>H</PartnerId>
          <ProgId>H00002</ProgId>
        </PartnerInformationGrp>
        <AmtInformationGrp>
          <HighAmt>19.00</HighAmt>
          <LowAmt>12.00</LowAmt>
        </AmtInformationGrp>
      </ProgramInfo>
      <ProgramInfo>
        <PartnerInformationGrp>
          <PartnerId>H</PartnerId>
          <ProgId>H00001</ProgId>
        </PartnerInformationGrp>
        <AmtInformationGrp>
          <HighAmt>1744.00</HighAmt>
          <LowAmt>99.00</LowAmt>
        </AmtInformationGrp>
      </ProgramInfo>

    </ProgramInfoList>
  </GetTestWSResponse>
</response>
is transformed into the result
Code:
<response>
   <GetTestWSResponse>
      <CardNumber>12345</CardNumber>
      <ProgramInfoList>
         <ProgramInfo>
            <PartnerInformationGrp>
               <PartnerId>H</PartnerId>
               <ProgId>H00002</ProgId>
            </PartnerInformationGrp>
            <AmtInformationGrp>
               <HighAmt>19999.00</HighAmt>
               <LowAmt>0.00</LowAmt>
            </AmtInformationGrp>
            <AmtInformationGrp>
               <HighAmt>19.00</HighAmt>
               <LowAmt>12.00</LowAmt>
            </AmtInformationGrp>
         </ProgramInfo>
         <ProgramInfo>
            <PartnerInformationGrp>
               <PartnerId>H</PartnerId>
               <ProgId>H00001</ProgId>
            </PartnerInformationGrp>
            <AmtInformationGrp>
               <HighAmt>24.00</HighAmt>
               <LowAmt>2.00</LowAmt>
            </AmtInformationGrp>
            <AmtInformationGrp>
               <HighAmt>1744.00</HighAmt>
               <LowAmt>99.00</LowAmt>
            </AmtInformationGrp>
         </ProgramInfo>
      </ProgramInfoList>
   </GetTestWSResponse>
</response>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old May 7th, 2012, 12:48 AM
Authorized User
 
Join Date: May 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Martin . the solution helped a lot.





Similar Threads
Thread Thread Starter Forum Replies Last Post
using current-grouping-key in a select statement mphare XSLT 3 September 2nd, 2011 05:07 PM
test if current-grouping-key() has changed? bonekrusher XSLT 4 October 24th, 2008 10:35 AM
grouping logic mike123 XSLT 7 October 27th, 2007 01:20 PM
Using key() and <xsl:key> freddy XSLT 2 January 18th, 2007 08:55 PM
KEY - GROUPING pallone XSLT 9 December 11th, 2006 06:19 AM





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