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 January 17th, 2012, 02:48 PM
Authorized User
 
Join Date: Feb 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Muenchian sort

I am attempting to use the muenchian sort on a list of invoices that are sorted by invoice number. What I am trying to do is outer sort by invoice number, then inner sort and copy
only the lineitem nodes that match by invoice number.

Here's my sample invoice list:

<INVOICELIST>
<INVOICE>
<INVNUM>111</INVNUM>
<LINEITEM>
<QTY>12</QTY>
<LINETOTAL>12</LINETOTAL>
</LINEITEM>
<INVOICETOTAL>12</INVOICETOTAL>
</INVOICE>
<INVOICE>
<INVNUM>112</INVNUM>
<LINEITEM>
<QTY>15</QTY>
<LINETOTAL>15</LINETOTAL>
</LINEITEM>
<INVOICETOTAL>19</INVOICETOTAL>
</INVOICE>
<INVOICE>
<INVNUM>112</INVNUM>
<LINEITEM>
<QTY>4</QTY>
<LINETOTAL>4</LINETOTAL>
</LINEITEM>
<INVOICETOTAL>19</INVOICETOTAL>
</INVOICE>
<INVOICE>
<INVNUM>113</INVNUM>
<LINEITEM>
<QTY>23</QTY>
<LINETOTAL>23</LINETOTAL>
</LINEITEM>
<INVOICETOTAL>23</INVOICETOTAL>
</INVOICE>
</INVOICELIST>

the stylesheet I'm trying to use:

Code:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<!-- Add an index (key) that allows selection based on the value of INVNUM -->
<xsl:key name="X" match="NODE" use="INVNUM"/>
<xsl:template match="/">
    
    <!-- The outer for-each selects the first node for every unique INVNUM value.  In other words, the outer for-each will iterate exactly once for each unique value of INVNUM -->
    <xsl:for-each select="//NODE[generate-id(.)=generate-id(key('X', INVNUM)[1])]">
        <xsl:sort select="INVNUM"/>
        <xsl:value-of select="INVNUM"/>
        <!-- The inner for-each iterates over all nodes that match the outer for-each INVNUM value -->
        <xsl:for-each select="key('X', INVNUM)">
            <xsl:sort select="LINEITEM"/>
            <xsl:value-of select="LINEITEM"/>
        </xsl:for-each>
    </xsl:for-each>
    
</xsl:template>
</xsl:stylesheet>
Desired output:

<INVOICELIST>
<INVOICE>
<INVNUM>111</INVNUM>
<LINEITEM>
<QTY>12</QTY>
<LINETOTAL>12</LINETOTAL>
</LINEITEM>
<INVOICETOTAL>12</INVOICETOTAL>
</INVOICE>
<INVOICE>
<INVNUM>112</INVNUM>
<LINEITEM>
<QTY>15</QTY>
<LINETOTAL>15</LINETOTAL>

</LINEITEM>
<LINEITEM>
<QTY>4</QTY>
<LINETOTAL>4</LINETOTAL>
</LINEITEM>
<INVOICETOTAL>19</INVOICETOTAL>
</INVOICE>

<INVOICE>
<INVNUM>113</INVNUM>
<LINEITEM>
<QTY>23</QTY>
<LINETOTAL>23</LINETOTAL>
</LINEITEM>
<INVOICETOTAL>23</INVOICETOTAL>
</INVOICE>
</INVOICELIST>

I get no output....

I'm running this from a batch like this:
msxsl.exe einvoices.xml muenchsort.xsl -o outfinal.xml

Any nudge in the right direction would be welcome.
 
Old January 20th, 2012, 12:32 PM
Authorized User
 
Join Date: Feb 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Muenchian sort - a start!

Here's what I've got so far.... it is getting me a sort and grouping the line items correctly... but now I need a way to get rid of excess INVOICE and child nodes when I've completed the grouping

Here's my adjusted working stylesheet
Code:
 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     version="1.0">
 <xsl:output method="xml" indent="no"/>
  
  <xsl:key name="invnumkey" match="INVOICE" use="INVNUM"/>
  <xsl:template match="/">
<INVOICELIST>
 
   <xsl:for-each select="//INVOICE[generate-id(.)=
        generate-id(key('invnumkey', INVNUM)[1])]">
     <xsl:sort select="invnum"/>
       <xsl:for-each select="key('invnumkey', INVNUM)">
       <INVOICE> 
        <xsl:if test="position() = 1">
     
        <INVNUM>
           <xsl:value-of select="INVNUM"/>
        </INVNUM>
        </xsl:if>
    <LINEITEM>
     <QTY>
        <xsl:value-of select="LINEITEM/QTY"/>
     </QTY>
     <LINETOTAL>
         <xsl:value-of select="LINEITEM/LINETOTAL"/>
     </LINETOTAL>
     </LINEITEM>
     <INVOICETOTAL>
          <xsl:value-of select="INVOICETOTAL"/> 
     </INVOICETOTAL>
     </INVOICE>
    </xsl:for-each> 
   </xsl:for-each> 
   
</INVOICELIST>
  </xsl:template>
</xsl:stylesheet>


<INVOICELIST>
<INVOICE>
<INVNUM>111</INVNUM>
<LINEITEM>
<QTY>12</QTY>
<LINETOTAL>12</LINETOTAL>
</LINEITEM>
<INVOICETOTAL>12</INVOICETOTAL>
</INVOICE>
<INVOICE>
<INVNUM>112</INVNUM>
<LINEITEM>
<QTY>15</QTY>
<LINETOTAL>15</LINETOTAL>
</LINEITEM>
<INVOICETOTAL>19</INVOICETOTAL>
</INVOICE>
<INVOICE>
<LINEITEM>
<QTY>4</QTY>
<LINETOTAL>4</LINETOTAL>
</LINEITEM>
<INVOICETOTAL>19</INVOICETOTAL>
</INVOICE>
<INVOICE>
<INVNUM>113</INVNUM>
<LINEITEM>
<QTY>23</QTY>
<LINETOTAL>23</LINETOTAL>
</LINEITEM>
<INVOICETOTAL>23</INVOICETOTAL>
</INVOICE>
</INVOICELIST>

I'm thinking maybe if I made a second pass with another stylesheet to remove extra nodes when the combined invoice pattern is found.... if anyone has a better way, I would love to hear it.

Thanks!
 
Old January 20th, 2012, 12:51 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I would do it like this:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="k1" match="INVOICE" use="INVNUM"/>

<xsl:template match="INVOICELIST">
  <xsl:copy>
    <xsl:apply-templates select="INVOICE[generate-id() = generate-id(key('k1', INVNUM)[1])]">
      <xsl:sort select="INVNUM" data-type="number"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

<xsl:template match="INVOICE">
  <xsl:copy>
    <xsl:copy-of select="INVNUM"/>
    <xsl:copy-of select="key('k1', INVNUM)/LINEITEM"/>
    <xsl:copy-of select="INVOICETOTAL"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old January 20th, 2012, 01:01 PM
Authorized User
 
Join Date: Feb 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Muenchian sort - solved!

Thanks Martin.... you saved me from marching down a path I didn't need to travel. That works great, no second pass required!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Muenchian grouping amhicraig XSLT 1 December 5th, 2007 06:43 PM
Dynamic sort order or sort datatype kapy_kal XSLT 2 September 18th, 2007 02:10 PM
Muenchian Grouping Chamkaur XSLT 1 June 21st, 2006 10:51 AM
how to sort cross tab.sort based on row total joxa83 Crystal Reports 7 March 2nd, 2006 09:12 AM
Unable to sort using xsl sort command sly_jimmy_boy XSLT 3 June 17th, 2005 05:15 AM





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