Altova Mailing List Archives


Re: [xsl] Nested loops

From: "G. Ken Holman" <gkholman@-------------------->
To:
Date: 11/13/2004 12:39:00 PM
At 2004-11-13 11:53 +0000, Peter Bradley wrote:
The problem is to create a list of invoices in a format something like 
this (all simplified as much as I can):



invoiceNumber: aaa
invoiceAddress: abc
product: xxx price: xxx.xx qty: xx linePrice: xxx.xx
...
product: xxx price: xxx.xx qty: xx linePrice: xxx.xx
invoiceTotal: xxxx.xx

invoiceNumber: bbb
invoiceAddress: def
product: yyy price: yyy.yy qty: yy linePrice: yyy.yy
...
product: yyy price: yyy.yy qty: yy linePrice: yyy.yy
invoiceTotal: yyyy.yy

and so on.



The xml file from which I have to produce this, is of the form:



<database>
        <invoice>
                <invoice_rec>
                        <invoiceNumber>aaa</invoiceNumber>
                        <invoiceAddress>abc</invoiceAddress>
                </invoice_rec>
                <invoice_rec>
                        ...
                </invoice_rec>
                ...
        </invoice>
        <invoice_line>
                <invoice_line_rec>
                        <invoiceNumber>aaa</invoiceNumber>
                        <product>xxx</product>
                        <price>xxx.xx</price>
                        <qty>xx</qty>
                        <linePrice>xxx.xx</linePrice>
                </invoice_line_rec>
                <invoice_line_rec>
                        ...
                </invoice_line_rec>
        </invoice_line>
        <invoice_total>
                <invoiceNumber>aaa</invoiceNumber>
                <invoiceTotal>xxxx.xx</invoiceTotal>
        </invoice_total>
</database>

So I need to pick up the first invoice and deal with its data, then pick 
up all the invoice lines for that invoice and deal with them, then pick up 
the invoice total for that invoice and deal with it.  Then I need to 
repeat the procedure for each succeeding invoice.



So I need some sort of nested loop.

Yes, nested "for-each" loops would do the trick simply if you don't have to 
deal with duplicates and determine only the unique ones from those.



Now, I take the point I learnt last week that this is probably best done 
using <xsl:apply-templates>,

Sure, that works too, a writer's choice of pull or push depends on how 
granular you want your stylesheet for later specialization, or how you want 
to use the template rules for other contexts.



but how can I just pick the node set that has the correct invoice 
number?  That is, I need to do something like:



*  apply the invoice_rec template

   Process "/database/invoice/invoice_rec"



*  print the contents

*  apply the invoice_line_rec template for nodes sharing an invoiceNumber 
with the invoice_rec it's "called" from

   Process "../../invoice_line/
                  invoice_line_rec[invoiceNumber=current()/invoiceNumber]"

*  apply the invoice_total template for the node sharing the invoice number

   Process "../../invoice_line/
                  invoice_total[invoiceNumber=current()/invoiceNumber]:

Do this for each invoice_rec.

Either pull:



   <xsl:for-each select="/database/invoice/invoice_rec">
      ...print the contents...
      <xsl:for-each select="../../invoice_line/
                  invoice_line_rec[invoiceNumber=current()/invoiceNumber]">
        ...print the line...
      </xsl:for-each>
      <xsl:for-each select="../../invoice_line/
                  invoice_total[invoiceNumber=current()/invoiceNumber]">
        ...print total...
      </xsl:for-each>
   </xsl:for_each>

Or push:



   <xsl:template match="/">
     <xsl:apply-templates select="/database/invoice/invoice_rec"/>
   </xsl:template>
   <xsl:template match="invoice_rec">
      ...print the contents...
      <xsl:apply-templates select="../../invoice_line/
                  invoice_line_rec[invoiceNumber=current()/invoiceNumber]"/>
      <xsl:apply-templates select="../../invoice_line/
                  invoice_total[invoiceNumber=current()/invoiceNumber]"/>
   </xsl:template>
   <xsl:template match="invoice_line_rec">
      ...print the line...
   </xsl:template>
   <xsl:template match="invoice_total">
      ...print the total...
   </xsl:template>

I've tried all sorts of things including predicates and variables, but I 
just can't seen to find a valid XPath expression that gives me the node 
set I need.

When you walk away from the current node in the evaluation of an XPath 
expression, the current() function can be used to "jump back" to where you 
started from the middle of the expression.



The above assumes you aren't having to deal with duplicates.



Put your focus on each invoice record and then address relatively from that 
record to all of the lines and the total whose reference is equal to the 
record you ware dealing with.



You can also do it with keys:



   <xsl:key name="recs" match="invoice_rec" use="'all'"/>
   <xsl:key name="lines" match="invoice_line" use="invoiceNumber"/>
   <xsl:key name="totals" match="invoice_total" use="invoiceNumber"/>

   <xsl:template match="/">
     <xsl:for-each select="key(recs,'all')">
       ...do contents...
       <xsl:for-each select="key(lines,invoiceNumber)">
         ...do line...
       </xsl:for-each>
       <xsl:for-each select="key(totals,invoiceNumber)">
         ...do total...
       </xsl:for-each>
     </xsl:for-each>
   </xsl:template>

I hope this helps.  It is untested since you didn't give any data to play with.



............................ Ken



--
World-wide on-site corporate, govt. & user group XML/XSL training.
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Breast Cancer Awareness  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.