XML/XSL Html table trouble
-
Hi Guys, wondering if you can help me again, I need to write a stylesheet for a xml document, i need to convert it to a html table with a header, i can get the header working and get the table created but i can't get the xml put within the table. the code is below do you any ideas, i thought this was quite a simple task but its turns out harder than i thought. xml sample - there is more to the code but this is a example, with the report is the title and other info but that is not relevent as the arguments fields is what i need to put into a table
<report-item-definition>
<arguments>
<argument>
<name>xVar</name>
<type>Column</type>
<default>@IPRED</default>
<display-name>X Axis Variable</display-name>
<description>X Axis Variable</description>
<visible>FALSE</visible>
<filter-type>@ALL</filter-type>
</argument>
<argument>
</report-item-definition>The table show look like the following Name - Type - Default - Display Name - Description - Visible - Filter Type At the moment my style sheet looks like this - but it does not seem to be working
<?xml version="1.0" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" ><xsl:template match ="/">
<html>
<head>
<title>XML Stylesheet</title>
</head><body> <h1 align="center"><b><xsl:value-of
select="report-item-definition/item-title" /></b></h1>
<xsl:apply-templates /> </body> </html>
</xsl:template>
<xsl:template match="report-item-definition/arguments">
<table width="700" border="1" align="center" style="font-family:verdana
font-size:10pt">
<tr bgcolor = "#cccccc"> <td width="130" align="center"><b>Name</b></td> <td align="center"><b>Type</b></td> <td align="center"><b>Default</b></td> <td align="center"><b>Display Name</b></td> <td align="center"><b>Description</b></td> <td align="center"><b>Visible</b></td> <td align="center"><b>Filter Type</b></td> </tr>
<xsl:for-each
-
Hi Guys, wondering if you can help me again, I need to write a stylesheet for a xml document, i need to convert it to a html table with a header, i can get the header working and get the table created but i can't get the xml put within the table. the code is below do you any ideas, i thought this was quite a simple task but its turns out harder than i thought. xml sample - there is more to the code but this is a example, with the report is the title and other info but that is not relevent as the arguments fields is what i need to put into a table
<report-item-definition>
<arguments>
<argument>
<name>xVar</name>
<type>Column</type>
<default>@IPRED</default>
<display-name>X Axis Variable</display-name>
<description>X Axis Variable</description>
<visible>FALSE</visible>
<filter-type>@ALL</filter-type>
</argument>
<argument>
</report-item-definition>The table show look like the following Name - Type - Default - Display Name - Description - Visible - Filter Type At the moment my style sheet looks like this - but it does not seem to be working
<?xml version="1.0" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" ><xsl:template match ="/">
<html>
<head>
<title>XML Stylesheet</title>
</head><body> <h1 align="center"><b><xsl:value-of
select="report-item-definition/item-title" /></b></h1>
<xsl:apply-templates /> </body> </html>
</xsl:template>
<xsl:template match="report-item-definition/arguments">
<table width="700" border="1" align="center" style="font-family:verdana
font-size:10pt">
<tr bgcolor = "#cccccc"> <td width="130" align="center"><b>Name</b></td> <td align="center"><b>Type</b></td> <td align="center"><b>Default</b></td> <td align="center"><b>Display Name</b></td> <td align="center"><b>Description</b></td> <td align="center"><b>Visible</b></td> <td align="center"><b>Filter Type</b></td> </tr>
<xsl:for-each
Your
xsl:for-each
executes in the scope of the current node, arguments. However, your XPath expression was designed to match a child arguments node of a parent arguments node:<xsl:for-each select="arguments/argument">
It should be:
<xsl:for-each select="argument">
since the argument node is a child of the arguments node.
"We make a living by what we get, we make a life by what we give." --Winston Churchill
-
Hi Guys, wondering if you can help me again, I need to write a stylesheet for a xml document, i need to convert it to a html table with a header, i can get the header working and get the table created but i can't get the xml put within the table. the code is below do you any ideas, i thought this was quite a simple task but its turns out harder than i thought. xml sample - there is more to the code but this is a example, with the report is the title and other info but that is not relevent as the arguments fields is what i need to put into a table
<report-item-definition>
<arguments>
<argument>
<name>xVar</name>
<type>Column</type>
<default>@IPRED</default>
<display-name>X Axis Variable</display-name>
<description>X Axis Variable</description>
<visible>FALSE</visible>
<filter-type>@ALL</filter-type>
</argument>
<argument>
</report-item-definition>The table show look like the following Name - Type - Default - Display Name - Description - Visible - Filter Type At the moment my style sheet looks like this - but it does not seem to be working
<?xml version="1.0" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" ><xsl:template match ="/">
<html>
<head>
<title>XML Stylesheet</title>
</head><body> <h1 align="center"><b><xsl:value-of
select="report-item-definition/item-title" /></b></h1>
<xsl:apply-templates /> </body> </html>
</xsl:template>
<xsl:template match="report-item-definition/arguments">
<table width="700" border="1" align="center" style="font-family:verdana
font-size:10pt">
<tr bgcolor = "#cccccc"> <td width="130" align="center"><b>Name</b></td> <td align="center"><b>Type</b></td> <td align="center"><b>Default</b></td> <td align="center"><b>Display Name</b></td> <td align="center"><b>Description</b></td> <td align="center"><b>Visible</b></td> <td align="center"><b>Filter Type</b></td> </tr>
<xsl:for-each
This is how I'd tend to write it - differences are:
- I've used CSS, rather than inline styles - my preference
- I've used the thead element for the header
- I've split up the templates a bit more - if you had more than one report-item-definition in an XML file, that would be useful :-) Also, it makes the context node more obvious, so you don't get issues like the one you had that George pointed out previously.
- I've used <xsl:apply-templates select="argument"/> rather than <xsl:for-each select="..."> to output each row of the table. My feeling is that using apply-templates like this is probably better idiomatic XSL than using for-each
<?xml version="1.0" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" ><xsl:template match ="/">
<html>
<style>
th { text-align: center }
thead { background-color: #cccccc }
h1 { text-align: center; font-weight: bold }
</style>
<head>
<title>XML Stylesheet</title>
</head><body> <xsl:apply-templates select="report-item-definition"/> </body> </html>
</xsl:template>
<xsl:template match="report-item-definition">
<h1><xsl:value-of select="item-title" /></h1><xsl:apply-templates select="arguments"/>
</xsl:template>
<xsl:template match="arguments">
<table width="700" border="1" align="center" style="font-family:verdana font-size:10pt"> <thead> <tr> <th width="130"><b>Name</b></th> <th><b>Type</b></th> <th><b>Default</b></th> <th><b>Display Name</b></th> <th><b>Description</b></th> <th><b>Visible</b></th> <th><b>Filter Type</b></th> </tr> </thead> <xsl:apply-templates select="argument"/> </table>
</xsl:template>
<xsl:template match="argument">
<tr>
<td><xsl:value-of select="name" /></td>
<!