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 June 28th, 2010, 07:55 AM
Authorized User
 
Join Date: Mar 2010
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
Default Count of distinct nodes in a group

Hi,

I am using XSL1.0 version

and in my xsl i am grouping with C_VAT_TAX_REG_NUM,C_TAX_REPORTING_NAME , C_BILLING_TP_SITE_NAME

Now i need to show the sum of C_FUNCTIONAL_TOTAL , and count of unique "C_TRX_NUMBER" that are falling in that group

my final xml out put should be like below

Code:
<VetaR c_vat="12345" total_sum="30" rec="2" />

After grouping C_VAT_TAX_REG_NUM,C_TAX_REPORTING_NAME , C_BILLING_TP_SITE_NAME
for C_TRX_NUMBER the value 100000244 has appeared two times and 100000245 has appeared one time.

so in my final out put , for "rec" it should show as "2" only ( i,e ..100000244 , 100000245 two values )

my xml

Code:
<?xml version="1.0" encoding="UTF-8" ?> 
<ZXXEUSL>
- <LIST_G_REP_PARAMETER_INFO>
- <G_REP_PARAMETER_INFO>
  <CP_ESL_EU_SERVICES_NAME>Services</CP_ESL_EU_SERVICES_NAME> 
  <CP_ESL_EU_ADDL_CODE1_NAME>Triangulation</CP_ESL_EU_ADDL_CODE1_NAME>
  <CF_LEDGER_CURRENCY>GBP</CF_LEDGER_CURRENCY>
</G_REP_PARAMETER_INFO>  
  </LIST_G_REP_PARAMETER_INFO>
- <LIST_G_REP_TRX_DETAIL_INFO>
  <G_REP_TRX_DETAIL_INFO>
  <C_TAX_REPORTING_CODE>02</C_TAX_REPORTING_CODE> 
  <C_VAT_TAX_REG_NUM>12345</C_VAT_TAX_REG_NUM> 
  <C_TAX_REPORTING_NAME>Triangulation</C_TAX_REPORTING_NAME> 
  <C_TRX_NUMBER>100000244</C_TRX_NUMBER> 
  <C_BILLING_TP_SITE_NAME>54321</C_BILLING_TP_SITE_NAME> 
  <C_FUNCTIONAL_TOTAL>10</C_FUNCTIONAL_TOTAL>
  </G_REP_TRX_DETAIL_INFO>  
  <G_REP_TRX_DETAIL_INFO>
  <C_TAX_REPORTING_CODE>02</C_TAX_REPORTING_CODE> 
  <C_VAT_TAX_REG_NUM>12345</C_VAT_TAX_REG_NUM> 
  <C_TAX_REPORTING_NAME>Triangulation</C_TAX_REPORTING_NAME> 
  <C_TRX_NUMBER>100000244</C_TRX_NUMBER> 
  <C_BILLING_TP_SITE_NAME>54321</C_BILLING_TP_SITE_NAME> 
  <C_FUNCTIONAL_TOTAL>10</C_FUNCTIONAL_TOTAL>
  </G_REP_TRX_DETAIL_INFO>  
  <G_REP_TRX_DETAIL_INFO>
  <C_TAX_REPORTING_CODE>02</C_TAX_REPORTING_CODE> 
  <C_VAT_TAX_REG_NUM>12345</C_VAT_TAX_REG_NUM> 
  <C_TAX_REPORTING_NAME>Triangulation</C_TAX_REPORTING_NAME> 
  <C_TRX_NUMBER>100000245</C_TRX_NUMBER> 
  <C_BILLING_TP_SITE_NAME>54321</C_BILLING_TP_SITE_NAME> 
  <C_FUNCTIONAL_TOTAL>10</C_FUNCTIONAL_TOTAL>
  </G_REP_TRX_DETAIL_INFO>  
  </LIST_G_REP_TRX_DETAIL_INFO>
  <CS_CURRENCY_CODE>GBP</CS_CURRENCY_CODE> 
  </ZXXEUSL>

Thanks
Anil
 
Old June 28th, 2010, 11:47 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You need to use a key concatenating the values of the different elements with a suitable separator (that separator should not occur in the element contents):
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
  <xsl:key name="k1" match="G_REP_TRX_DETAIL_INFO" use="concat(C_VAT_TAX_REG_NUM, '|', C_TAX_REPORTING_NAME, '|', C_BILLING_TP_SITE_NAME)"/>
  <xsl:key name="k2" match="G_REP_TRX_DETAIL_INFO" use="concat(C_VAT_TAX_REG_NUM, '|', C_TAX_REPORTING_NAME, '|', C_BILLING_TP_SITE_NAME, '|', C_TRX_NUMBER)"/>
  
  <xsl:template match="/">
    <xsl:apply-templates select="//G_REP_TRX_DETAIL_INFO[generate-id() = generate-id(key('k1', concat(C_VAT_TAX_REG_NUM, '|', C_TAX_REPORTING_NAME, '|', C_BILLING_TP_SITE_NAME))[1])]"/>
  </xsl:template>
  
  <xsl:template match="G_REP_TRX_DETAIL_INFO">
    <VetaR 
      c_vat="{C_VAT_TAX_REG_NUM}"
      total_sum="{sum(key('k1', concat(C_VAT_TAX_REG_NUM, '|', C_TAX_REPORTING_NAME, '|', C_BILLING_TP_SITE_NAME))/C_FUNCTIONAL_TOTAL)}"
      rec="{count(key('k1', concat(C_VAT_TAX_REG_NUM, '|', C_TAX_REPORTING_NAME, '|', C_BILLING_TP_SITE_NAME))[generate-id() = generate-id(key('k2', concat(C_VAT_TAX_REG_NUM, '|', C_TAX_REPORTING_NAME, '|', C_BILLING_TP_SITE_NAME, '|', C_TRX_NUMBER))[1])])}"/>
  </xsl:template>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old June 28th, 2010, 11:51 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Maybe it is clearer to "compute" the "current group" into a variable so here is an adaption of above stylesheet that does that:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
  <xsl:key name="k1" match="G_REP_TRX_DETAIL_INFO" use="concat(C_VAT_TAX_REG_NUM, '|', C_TAX_REPORTING_NAME, '|', C_BILLING_TP_SITE_NAME)"/>
  <xsl:key name="k2" match="G_REP_TRX_DETAIL_INFO" use="concat(C_VAT_TAX_REG_NUM, '|', C_TAX_REPORTING_NAME, '|', C_BILLING_TP_SITE_NAME, '|', C_TRX_NUMBER)"/>
  
  <xsl:template match="/">
    <xsl:apply-templates select="//G_REP_TRX_DETAIL_INFO[generate-id() = generate-id(key('k1', concat(C_VAT_TAX_REG_NUM, '|', C_TAX_REPORTING_NAME, '|', C_BILLING_TP_SITE_NAME))[1])]"/>
  </xsl:template>
  
  <xsl:template match="G_REP_TRX_DETAIL_INFO">
    <xsl:variable name="current-group" 
      select="key('k1', concat(C_VAT_TAX_REG_NUM, '|', C_TAX_REPORTING_NAME, '|', C_BILLING_TP_SITE_NAME))"/>
    <VetaR 
      c_vat="{C_VAT_TAX_REG_NUM}"
      total_sum="{sum($current-group/C_FUNCTIONAL_TOTAL)}"
      rec="{count($current-group[generate-id() = generate-id(key('k2', concat(C_VAT_TAX_REG_NUM, '|', C_TAX_REPORTING_NAME, '|', C_BILLING_TP_SITE_NAME, '|', C_TRX_NUMBER))[1])])}"/>
  </xsl:template>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
anarleti (June 30th, 2010)
 
Old June 30th, 2010, 03:39 AM
Authorized User
 
Join Date: Mar 2010
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Hi Martin,

Your solution is working perfect ... thanks for your reply...
Your way of representing the solution also appreciated.

Thanks and Regards,
Jyotsna Sajja.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Get distinct values on *second* grouping of nodes. MissHenesy XSLT 4 October 23rd, 2006 10:34 AM
Count Distinct Jonas Access 2 August 14th, 2006 01:03 PM
count distinct values Chris Cash XSLT 3 June 8th, 2006 04:55 PM
count distinct nodes alexshiell XSLT 2 January 27th, 2005 11:19 AM
Distinct or Group By? sprion Access 9 January 13th, 2005 09:31 PM





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