Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested FOR EACH XSLT how to?

Status
Not open for further replies.

Zargo

Programmer
Mar 21, 2005
109
Dear all,

I have a XML Invoice like(=example):

<?xml version="1.0" encoding="UTF-8" ?>
- <Invoice xmlns:hrx="+ <Header>
- <Line>
<LineNumber>1</LineNumber>
<TotalAmount currency="EUR">+196.76</TotalAmount>
+ <Tax>
- <Line>
<LineNumber>10</LineNumber>
<TotalAmount currency="EUR">+196.76</TotalAmount>
<Description>Regular</Description>
+ <Price>
<ItemQuantity uom="Regular">+14.85</ItemQuantity>
</Line>
</Line>
+ <Line>
+ <Line>
</Invoice>

I have made a XSL and want to show all INVOICE\LINE and INVOICE\LINE\LINE items in a HTML table but without success. My XSL looks like:

<table border="0">

<tr bgcolor="#c0c0c0">
<th align="left">Linenumber</th>
<th align="left">Name</th>
<th align="left">Description</th>
<th align="left">Itemquantity</th>
<th align="left">Amount</th>
<th align="left">Multiplier</th>
<th align="left">Totalamount</th>
<th align="left">VAT %</th>
<th align="left">Linenumber header</th>
</tr>

<xsl:for-each select="Invoice/Line/Line">
<tr>
<td><xsl:value-of select="LineNumber"/></td>
<td><xsl:value-of select="UserArea/tc:Timecard/tc:ReportedResource/tc:person/tc:LegalName"/></td>
<td><xsl:value-of select="UserArea/tc:Timecard/tc:ReportedTime/tc:TimeInterval/@type"/></td>
<td><xsl:value-of select="ItemQuantity"/></td>
<td><xsl:value-of select="Price/Amount"/></td>
<td><xsl:value-of select="UserArea/tc:Timecard/tc:ReportedTime/tc:TimeInterval/tc:RateOrAmount/@Multiplier"/></td>
<td><xsl:value-of select="TotalAmount"/></td>
<td><xsl:value-of select="//Invoice/Line/Tax/PercentQuantity"/></td>
<td><xsl:value-of select="//Invoice/Line/Linenumber"/></td>
</tr>
</xsl:for-each>
</table>


I have problems with the last two columns in my table:
<td><xsl:value-of select="//Invoice/Line/Tax/PercentQuantity"/></td> <td><xsl:value-of select="//Invoice/Line/Linenumber"/></td>

This two are not in //invoice/line/line but a level higher this info is in //invoice/line, how to get this info. Now i'm getting the following table:

10|A.R. SONAI|Regular|+21.57|+13.25|+0.00 |+285.80|19.00|1
21|A.R. SONAI| | | |+17.70| |19.00|1
10|A.C. REID |Regular|+21.23|+18.25|+0.00 |+87.45 |19.00|1
21|A.C. REID | | | | |+23.60 |19.00|1
10|A. WITH |Regular|+20.28|+19.00|+0.00 |+38.32 |19.00|1

Check the last two columns. I think i'm looping wrong(i must first loop into invoice\line and then in invoice\line\line). It gets only the first vat % and the linenumber header. How can i get the following lay-out?

10|A.R. SONAI|Regular|+21.57|+13.25|+0.00 |+285.80|19.00|1
21|A.R. SONAI| | | |+17.70| |19.00|1
10|A.C. REID |Regular|+21.23|+18.25|+0.00 |+87.45 | 6.00|2
21|A.C. REID | | | | |+23.60 | 6.00|2
10|A. WITH |Regular|+20.28|+19.00|+0.00 |+38.32 | 6.00|3

A big star for the golden tip.

TIA
 
Do this within the for-each loop? (I use variable to more clearly isolate xpath.)
[tt]
<xsl:for-each select="Invoice/Line/Line">
[blue]<xsl:varable "x" select=".." />[/blue]
<tr>
<!-- etc etc followed by your script lines on td-->
[/tt]
Then at the last two columns.
[tt]
<!-- etc etc preceded by your script lines on td -->
<td><xsl:value-of select="[blue]$x[/blue]/Tax/PercentQuantity"/></td>
<td><xsl:value-of select="[blue]$x[/blue]/Line[red]N[/red]umber"/></td>
</tr>
</xsl:for-each>
[/tt]
 
Hi tsuji,

I have tried your approach, but it doesn't work. I couldnt generate a html page:

<xsl:for-each select="Invoice/Line/Line">
<xsl:variable name="x" select="Invoice/Line" />
<tr>
<!-- etc etc all Invoice/LINE/LINE elements-->

Then the last two columns/elements from the Invoice/LINE element
<!-- etc etc preceded by your script lines on td -->
<td><xsl:value-of select="$x/Tax/PercentQuantity"/></td>
<td><xsl:value-of select="$x/LineNumber"/></td>
</tr>
</xsl:for-each>

Without successss. Do u know what i'm doing wrong.
 
Zarqo, sorry I had a typo in my post and you had noticed it and put right. My posted line
[tt]> <xsl:varable "x" select=".." />[/tt]
should of course be read as:
[tt] <xsl:varable [red]name=[/red]"x" select=".." />[/tt]

However, you had change the select part! Could you keep the notion of matching node's parent?
[tt]> select=".."[/tt]
You have changed it to
[tt]>> select="invoice/line"[/tt]
which means something else. It means by default in the context
[tt] child::invoice/line[/tt]
or absolute path /Invoice/Line/Line/invoice/line. There isn't anything like that. (Also watch case sensitivity too.)
 
Tsuji,

Thanks a lot for your support and replies. But it still doesnt work. You had also mistyped the sentence:
<xsl:varable name="x" select=".." />
must be
<xsl:variable name="x" select=".." />

after this change also i couldnt get the right info. I will paste the hole for each section here, could you see what im doing wrong?:
Code:
<xsl:for-each select="Invoice/Line/Line"> 
     <xsl:variable name="x" select=".." />  
      <tr>
        <td><xsl:value-of select="TotalAmount"/></td>
      	<td><xsl:value-of select="UserArea/tc:Timecard/tc:ReportedResource/tc:Person/tc:LegalName"/></td>
        <td><xsl:value-of select="UserArea/tc:Timecard/tc:ReportedTime/tc:TimeInterval/@type"/></td>
        <td><xsl:value-of select="ItemQuantity"/></td>
        <td><xsl:value-of select="Price/Amount"/></td>
        <td><xsl:value-of select="UserArea/tc:Timecard/tc:ReportedTime/tc:TimeInterval/tc:RateOrAmount/@Multiplier"/></td>
        <td><xsl:value-of select="$x/Tax/PercentQuantity"/></td>
        <td><xsl:value-of select="$x/LineNumber"/></td>
      </tr>
    </xsl:for-each>

Thanks a lot...






 
I make a test xml with artificial data. I don't know your tc namespace. With unsubstantiated hrx, I get the display of data all right, for the last two columns and other manageable columns.

What is the error if any shown in ff?
 
Tsuji,

Can i send my xml and xsl files by mail?

Hope to hear from you soon.

Thanks in advance.
 
Zargo, I'm afraid it's a no, it is contrary to the working principle of tek-tips as a public forum with any advice open to peers review.

I can show you though the test setup. I've made a dumb test page like this.
[tt]
<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<Invoice xmlns:hrx=" <Header>
<childx>...</childx>
</Header>
<Line>
<LineNumber>1</LineNumber>
<TotalAmount currency="EUR">+196.76</TotalAmount>
<Tax>
<PercentQuantity>15.00</PercentQuantity>
</Tax>
<Line>
<LineNumber>10</LineNumber>
<TotalAmount currency="EUR">+196.76</TotalAmount>
<Description>Regular</Description>
<Price>
<something2>...</something2>
</Price>
<ItemQuantity uom="Regular">+14.85</ItemQuantity>
</Line>
</Line>
<Line>
<!-- I fill artificial -->
<LineNumber>2</LineNumber>
<TotalAmount currency="EUR">+296.76</TotalAmount>
<Tax>
<PercentQuantity>25.00</PercentQuantity>
</Tax>
<Line>
<LineNumber>20</LineNumber>
<TotalAmount currency="EUR">+296.76</TotalAmount>
<Description>Regular</Description>
<Price>
<something2>...</something2>
</Price>
<ItemQuantity uom="Regular">+24.85</ItemQuantity>
</Line>
</Line>
<Line>
<!-- I fill artificial -->
<LineNumber>3</LineNumber>
<TotalAmount currency="EUR">+396.76</TotalAmount>
<Tax>
<PercentQuantity>35.00</PercentQuantity>
</Tax>
<Line>
<LineNumber>30</LineNumber>
<TotalAmount currency="EUR">+396.76</TotalAmount>
<Description>Regular</Description>
<Price>
<something2>...</something2>
</Price>
<ItemQuantity uom="Regular">+34.85</ItemQuantity>
</Line>
</Line>
</Invoice>
[/tt]
The test.xsl looks like this.
[tt]
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="<xsl:template match="/">
<html>
<body>
<table border="0">
<tr bgcolor="#c0c0c0">
<th align="left">Linenumber</th>
<th align="left">Name</th>
<th align="left">Description</th>
<th align="left">Itemquantity</th>
<th align="left">Amount</th>
<th align="left">Multiplier</th>
<th align="left">Totalamount</th>
<th align="left">VAT %</th>
<th align="left">Linenumber header</th>
</tr>

<xsl:for-each select="Invoice/Line/Line">
<xsl:variable name="x" select=".." />
<tr>
<td><xsl:value-of select="LineNumber"/></td>
<!--
<td><xsl:value-of select="UserArea/tc:Timecard/tc:ReportedResource/tc:person/tc:LegalName"/></td>
<td><xsl:value-of select="UserArea/tc:Timecard/tc:ReportedTime/tc:TimeInterval/@type"/></td>
-->
<td></td>
<td></td>
<td><xsl:value-of select="ItemQuantity"/></td>
<td><xsl:value-of select="Price/Amount"/></td>
<!--
<td><xsl:value-of select="UserArea/tc:Timecard/tc:ReportedTime/tc:TimeInterval/tc:RateOrAmount/@Multiplier"/></td>
-->
<td></td>
<td><xsl:value-of select="TotalAmount"/></td>
<td><xsl:value-of select="$x/Tax/PercentQuantity"/></td>
<!--
<td><xsl:value-of select="LineNumber"/></td>
-->
<td><xsl:value-of select="$x/LineNumber"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
[/tt]
It shows for instance 1,2,3 at the last column etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top