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!

Showing hashdata on HTML table with XSL 1

Status
Not open for further replies.

comma

Programmer
Feb 12, 2003
24
FI
Hi,

is it possible to show the following structure (made with XML::Dumper in Perl) in HTML table with XSL, so that item under arrayref would be its own row in html table:

<!DOCTYPE xml>
<?xml-stylesheet href="test.xsl" type="text/xsl"?>
<perldata>
<hashref>
<item key="res">
<arrayref memory_address="0x1d85438">
<item key="0">1</item>
<item key="1">2</item>
<item key="2">3</item>
</arrayref>
</item>
<item key="res2">
<arrayref memory_address="0x1d85439">
<item key="0">1</item>
<item key="1">2</item>
<item key="2">3</item>
</arrayref>
</item>
</hashref>
</perldata>

I tried this, but it doesn't work correctly (how to apply template4 for item, when arrayref template is run?

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:eek:utput method="html" version="1.0" encoding="iso-8859-1" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="perldata">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="hashref">
<table border="1">
<xsl:apply-templates />
</table>
</xsl:template>

<xsl:template match="arrayref">
<tr>
<td>
<xsl:apply-templates />
</td>
</tr>
</xsl:template>

<xsl:template match="item">
<td>
<xsl:copy-of select="." />
</td>
</xsl:template>

</xsl:stylesheet>

Br, Comma
 
You 've got nodes named <item> on two levels:
childnode of <hashref>, and childnodes of <arrayref>.
When your stylesheet says:
<xsl:template match="hashref">
<table border="1">
<xsl:apply-templates />
</table>
</xsl:template>
the nodes below <hashref> (which are <item>-nodes are parsed with:
<xsl:template match="item">
<td>
<xsl:copy-of select="." />
</td>
</xsl:template>
which is probably not what you had in mind.

<xsl:template match="hashref/item">
<table border="1">
<xsl:apply-templates />
</table>
</xsl:template>
would be a simple way to fix that.

Another thing is: <xsl:copy-of select="."> copies the whole node into in your output.
Usually you would't want <td><item key="0">1</item></td> in your html.
If you use <xsl:value-of select="."> you get
<td>1</td>

Good luck,
 
Thank you jel, that solved the issue. And I learnt also more about XSL :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top