[xsl] Nested loops

Subject: [xsl] Nested loops
From: Peter Bradley <apvx95@xxxxxxxxxxxxx>
Date: Sat, 13 Nov 2004 11:53:26 +0000
Hi people.

I'm sorry to impose upon your good natures, but I wonder if you could help me again. As before, I want to be up front about the fact that this question arises from an exercise set as part of a course module, and to assure you that any help I get will be noted in the answer I submit (as it was last week: url in a previous post, but I'll supply it again if anyone wants it).

Please believe me as well, when I say that I've tried everything I know to solve this on my own, but without success after 3 days solid (and I only have 7 to solve this and 3 other problems - and I have a programming job to hold down).

Could you also bear in mind in your answers that I have all of 10 days experience with XSL! Please be merciful :-)

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.

Now, I take the point I learnt last week that this is probably best done using <xsl:apply-templates>, 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
* print the contents
* apply the invoice_line_rec template for nodes sharing an invoiceNumber with the invoice_rec it's "called" from
* apply the invoice_total template for the node sharing the invoice number


Do this for each invoice_rec.

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.

If anyone can find the time to help with this I'd be extremely grateful.

Thanks in advance

Peter

Current Thread