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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT two tables.

Status
Not open for further replies.

shiyam198

MIS
Nov 26, 2007
8
DE
Hi,

I have spent a couple of hours on this. Really puzzling for newbie me. I am sure its something simple i am missing here.

Please see the attached for the full code.

But basically I am trying to create two tables.

First table - displays the data under "greeting/Item". (with a for-each loop)
Second table - displays the data under value of "nCustomerMarketing/ndata7/@value"

When i use this, it only displays the second table and the first table vanishes. When i comment out the second "<xsl:template", the first table shows up. Code is below and the XML file is attached in Here is the code.

Thanks in advance for your help

<xsl:stylesheet xmlns:xsl=" version="1.0">
<xslutput method="html"/>


<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="xml"/>
</body>
</html>

</xsl:template>

<xsl:template match="nroot">

<table border="1">
<xsl:for-each select="greeting/Item">

<tr>
<td>
<xsl:value-of select="npaymentmethod_name"/>
</td>

<td>
<xsl:value-of select="npaymentmethod_type"/>
</td>
</tr>

</xsl:for-each>
</table>

<br/>
</xsl:template>


<xsl:template match="nroot">

<table border="1">
<tr>
<td>
<xsl:value-of select="nCustomerMarketing/ndata7/@value"/>
</td>
</tr>
</table>

</xsl:template>


</xsl:stylesheet>
 
[1] There is rule of priority in case same matching templates are found. To alleviate this and use them all, add a unique mode attribute to them.
[tt]
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="xml[blue]/nroot[/blue]" [red]mode="A"[/red] />
<xsl:apply-templates select="xml[blue]/nroot[/blue]" [red]mode="B"[/red] />
</body>
</html>
</xsl:template>
[/tt]
[1.1] I would much prefer to be more precise matching "xml/nroot" instead of "xml" so that it won't leave place to the caprice in case the documents xml and xsl are developed further.
[1.2] I would avoid using xml or anything like that to as element name.

[2] Then add mode attribute to each of the templates, like this for the first (mode A) and same for the second (mode B).
[tt] <xsl:template match="nroot" [red]mode="A"[/red]>[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top