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 December 1st, 2008, 12:56 PM
Authorized User
 
Join Date: Jun 2008
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default Top 9 + Sum of others

Hi guys,

I hope you all had a wonderful holidays!

Got another question, I have an xml like this:

<?xml version="1.0"?>
<result>
    <tabledata>
        <row data01="Department 01" data02="10800"/>
        <row data01="Department 02" data02="10600"/>
        <row data01="Department 03" data02="10900"/>
        <row data01="Department 04" data02="11000"/>
        <row data01="Department 05" data02="10500"/>
        <row data01="Department 06" data02="11100"/>
        <row data01="Department 07" data02="11300"/>
        <row data01="Department 08" data02="10400"/>
        <row data01="Department 09" data02="11700"/>
        <row data01="Department 10" data02="11200"/>
        <row data01="Department 11" data02="10300"/>
        <row data01="Department 12" data02="11400"/>
        <row data01="Department 13" data02="11600"/>
        <row data01="Department 14" data02="10200"/>
        <row data01="Department 15" data02="11500"/>
        <row data01="Department 16" data02="10700"/>
        <row data01="Department 17" data02="10100"/>
  </tabledata>
</result>

What I want is an XSLT to:
1) Get Top 9 records by data02
2) Then sum all other records on data02 into the 10th record
3) Sort final result by data02.

The final result should looks like:
<?xml version="1.0"?>
<result>
    <tabledata>
        <row data01="ALL OTHERS" data02="83600"/>
        <row data01="Department 09" data02="11700"/>
        <row data01="Department 13" data02="11600"/>
        <row data01="Department 15" data02="11500"/>
        <row data01="Department 12" data02="11400"/>
        <row data01="Department 07" data02="11300"/>
        <row data01="Department 10" data02="11200"/>
        <row data01="Department 06" data02="11100"/>
        <row data01="Department 04" data02="11000"/>
        <row data01="Department 03" data02="10900"/>
    </tabledata>
</result>

It requires multiple steps in my mind which beyond my xslt knowledge. So thank very much for pointing me the direction!


George Meng
__________________
George Meng
 
Old December 1st, 2008, 01:00 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Do you want to use XSLT 2.0 or 1.0?

--
  Martin Honnen
  Microsoft MVP - XML
 
Old December 1st, 2008, 01:03 PM
Authorized User
 
Join Date: Jun 2008
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hi Martin,

I render all XSLT via Coldfusion 8, I think it only support XSLT 1.0

Thanks very much!

George Meng
 
Old December 1st, 2008, 01:08 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>I hope you all had a wonderful holidays!

I have to tell you something you may find surprising - we are not all Americans.

Are you using XSLT 1.0 or 2.0? This one (like so many others...) is easier with 2.0 because you can put a sorted sequence in a variable. Like this:

<xsl:variable name="sorted-rows" as="element(row)*">
  <xsl:perform-sort select="row">
    <xsl:sort select="xs:integer(data02)"/>
  </xsl:perform-sort>
</xsl:variable>

<row data="ALL OTHERS" data02="sum($sorted-rows[position() gt 9])"/>
<xsl:copy-of select="reverse($sorted-rows[position() le 9])"/>

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old December 1st, 2008, 01:17 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is an XSLT 2.0 solution, I don't have time now for an XSLT 1.0 solution:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xsd"
  version="2.0">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="tabledata">
    <xsl:copy>
      <xsl:variable name="sorted-rows" as="element()*">
        <xsl:perform-sort select="row">
          <xsl:sort select="number(@data02)" order="descending"/>
        </xsl:perform-sort>
      </xsl:variable>
      <xsl:variable name="new-rows" as="element()*">
        <xsl:sequence select="$sorted-rows[position() &lt; 10]"/>
        <row data01="ALL OTHERS" data02="{sum($sorted-rows[position() &gt; 9]/@data02)}"/>
      </xsl:variable>
      <xsl:apply-templates select="$new-rows">
        <xsl:sort select="number(@data02)" order="descending"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
If you still need and XSLT 1.0 solution or need help converting the XSLT 2.0 solution to an XSLT 1.0 solution then let us know whether your XSLT 1.0 processor supports an extension function to convert a result tree fragment to a node-set (like the EXSLT node-set function).

--
  Martin Honnen
  Microsoft MVP - XML
 
Old December 1st, 2008, 01:43 PM
Authorized User
 
Join Date: Jun 2008
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hi Martin and Michael,

I tested again, Coldfusion 8 XSLT engine only support XSLT 1.

As a developer, I can understand the Martin's code, but have no idea how to convert it to XLST 1.0.

I guess I don't know how to do multiple step convertion in XSLT 1.0

Thanks very much!


George Meng
 
Old December 1st, 2008, 01:58 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I'm afraid those of us who use XSLT 2.0 every day find coding within the limitations of 1.0 a distinctly unenjoyable experience. This one can be done though - provided you have access to an xx:node-set() extension function. The logic is

(a) create a variable (a result tree fragment in 1.0 terms) holding the sorted nodes:

<xsl:variable name="x">
  <xsl:for-each select="row">
    <xsl:sort ...
    <xsl:copy-of select="."/>
</xsl:variable>

(b) turn this into a node-set

<xsl:variable name="sorted-nodes" select="xx:node-set($x0)/row"/>

(c) process these nodes

<row ... data02="{sum($sorted-nodes[position() > 9])}"/>

<xsl:for-each select="$sorted-nodes[position() &lt;= 9]">
   ...
</xsl:for-each>

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old December 1st, 2008, 03:09 PM
Authorized User
 
Join Date: Jun 2008
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hi Michael,

Follow your direction, I build such xslt:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:exsl="http://exslt.org/common"
  version="1.0">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="tabledata">
    <xsl:variable name="x">
      <xsl:for-each select="row">
        <xsl:sort select="data02" data-type="number" order="descending"/>
        <xsl:copy-of select="."/>
      </xsl:for-each>
    </xsl:variable>
    <xsl:variable name="sorted-nodes" select="exsl:node-set($x)/row"/>
    <row data02="{sum($sorted-nodes[position() > 9])}"/>
    <xsl:for-each select="$sorted-nodes[position() &lt;= 9]">
      <xsl:copy-of select="$sorted-nodes[position()]/row/data01"/>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

However, I got this result:

<?xml version="1.0" encoding="UTF-8"?>
<result>
    <row xmlns:exsl="http://exslt.org/common" data02="NaN"/>
</result>

Any thoughts?

Thanks very much!

George Meng
 
Old December 1st, 2008, 03:46 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I think it should be

<row data02="{sum($sorted-nodes[position() > 9]/@data02)}"/>


Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old December 1st, 2008, 06:13 PM
Authorized User
 
Join Date: Jun 2008
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hi Michael,

That solved my question.

Thanks a lot!

George Meng





Similar Threads
Thread Thread Starter Forum Replies Last Post
SELECT SUM Top x CassyFrost Classic ASP Basics 3 October 16th, 2007 08:38 AM
Help: Running Sum (or Cumulative Sum) timdasa VB Databases Basics 1 August 22nd, 2006 03:12 PM
SELECT TOP n rgerald SQL Server 2000 3 May 12th, 2006 04:03 PM
SELECT TOP n NOT SELECTING TOP n! ibi SQL Language 8 March 30th, 2005 08:08 PM
SELECT TOP ciko1973 SQL Language 5 August 8th, 2003 04:35 AM





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