bjh1977
asked on
XSL File - Displaying Column Header and Data Only If Node Exists
Hi,
I've got a two XML files. One contains a node called <OrganisationHeader>, the other one doesn't. With the Help of Gertone (thanks!) I've now got a stylesheet that will display the XML data in tabular format regardless of the existence or otherwise of the <OrganisationHeader> node.
Now I'm trying to only display those nodes that exist, and with my very limited knowledge of XSL I've manged to make this worj for the actual data. Now I'm trying to do the sdame with the table headers.
Below I've included the two XML files (one with, one without the <OrganisationHeader> node) and the XSL file in its current state. Any advice on how to conditionally show the column headings, based on whether or not the node it referes to exists?
Thanks.
I've got a two XML files. One contains a node called <OrganisationHeader>, the other one doesn't. With the Help of Gertone (thanks!) I've now got a stylesheet that will display the XML data in tabular format regardless of the existence or otherwise of the <OrganisationHeader> node.
Now I'm trying to only display those nodes that exist, and with my very limited knowledge of XSL I've manged to make this worj for the actual data. Now I'm trying to do the sdame with the table headers.
Below I've included the two XML files (one with, one without the <OrganisationHeader> node) and the XSL file in its current state. Any advice on how to conditionally show the column headings, based on whether or not the node it referes to exists?
Thanks.
XSL File So Far:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Test Result</h2>
<table border="1" cellpadding="3">
<tbody>
<tr>
<td>RevenueCentre</td>
<td>SalesDate</td>
<td>Location</td>
<td>PLU</td>
<td>CategoryCode</td>
<td>Quantity</td>
<td>Description</td>
<td>SaleType</td>
<td>GrossSalesPrice</td>
<td>NetSalesPrice</td>
<td>TotalGrossSales</td>
<td>TotalNetSales</td>
<td>TotalVAT</td>
</tr>
<xsl:apply-templates select=".//SalesTransaction" />
</tbody>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="SalesTransaction">
<tr>
<xsl:apply-templates select="ancestor::SalesHeader" />
<td>
<xsl:value-of select="PLU" />
</td>
<xsl:if test="count(CategoryCode) > 0">
<td>
<xsl:value-of select="CategoryCode" />
</td>
</xsl:if>
<td>
<xsl:value-of select="Quantity" />
</td>
<td>
<xsl:value-of select="Description" />
</td>
<xsl:if test="count(SaleType) > 0">
<td>
<xsl:value-of select="SaleType" />
</td>
</xsl:if>
<xsl:if test="count(GrossSalesPrice) > 0">
<td>
<xsl:value-of select="GrossSalesPrice" />
</td>
</xsl:if>
<xsl:if test="count(NetSalesPrice) > 0">
<td>
<xsl:value-of select="NetSalesPrice" />
</td>
</xsl:if>
<xsl:if test="count(TotalGrossSales) > 0">
<td>
<xsl:value-of select="TotalGrossSales" />
</td>
</xsl:if>
<xsl:if test="count(TotalNetSales) > 0">
<td>
<xsl:value-of select="TotalNetSales" />
</td>
</xsl:if>
<xsl:if test="count(TotalVat) > 0">
<td>
<xsl:value-of select="TotalVAT" />
</td>
</xsl:if>
</tr>
</xsl:template>
<xsl:template match="SalesHeader">
<xsl:if test="count(RevenueCentre) > 0">
<td>
<xsl:value-of select="RevenueCentre" />
</td>
</xsl:if>
<td>
<xsl:value-of select="Location" />
</td>
<td>
<xsl:value-of select="SalesDate" />
</td>
</xsl:template>
</xsl:stylesheet>
XML File 1 (includes <OrganisationHeader> node):
<Header>
<OrganisationHeader>
<OrganisationID>12345</OrganisationID>
<SalesHeader>
<SalesDate>13/02/2009 16:44:17</SalesDate>
<Location>47</Location>
<SalesTransaction>
<PLU>1234</PLU>
<CategoryCode>4671</CategoryCode>
<Description>Sample Description dfds </Description>
<GrossSales>2.1500</GrossSales>
<TotalGrossSales>21.25</TotalGrossSales>
<Quantity>1</Quantity>
<SaleType>9</SaleType>
<RevenueCentre>123</RevenueCentre>
</SalesTransaction>
<SalesTransaction>
<PLU>2345</PLU>
<CategoryCode>4671</CategoryCode>
<Description>Sample Description 1</Description>
<GrossSales>4.1500</GrossSales>
<TotalGrossSales>21.25</TotalGrossSales>
<Quantity>1</Quantity>
<SaleType>10</SaleType>
<RevenueCentre>123</RevenueCentre>
</SalesTransaction>
<SalesTransaction>
<PLU>64356</PLU>
<CategoryCode>4671</CategoryCode>
<Description>Sample Something 1</Description>
<GrossSales>4.1500</GrossSales>
<TotalGrossSales>21.25</TotalGrossSales>
<Quantity>1</Quantity>
<SaleType>10</SaleType>
<RevenueCentre>123</RevenueCentre>
</SalesTransaction>
<SalesTransaction>
<PLU>67657</PLU>
<CategoryCode>4671</CategoryCode>
<Description>Sample Desc 1</Description>
<GrossSales>4.1500</GrossSales>
<TotalGrossSales>21.25</TotalGrossSales>
<Quantity>1</Quantity>
<SaleType>10</SaleType>
<RevenueCentre>123</RevenueCentre>
</SalesTransaction>
</SalesHeader>
</OrganisationHeader>
</Header>
XML File 2 (does not include <OrganisationHeader> node):
<Header>
<OrganisationID>12345</OrganisationID>
<SalesHeader>
<SalesDate>13/02/2009 16:44:17</SalesDate>
<Location>47</Location>
<SalesTransaction>
<PLU>1234</PLU>
<Description>Sample Description dfds </Description>
<GrossSales>2.1500</GrossSales>
<TotalNetSales>21.25</TotalNetSales>
<Quantity>1</Quantity>
<SaleType>9</SaleType>
<RevenueCentre>123</RevenueCentre>
<TotalVAT>1.25</TotalVAT>
</SalesTransaction>
<SalesTransaction>
<PLU>2345</PLU>
<Description>Sample Description 1</Description>
<GrossSales>4.1500</GrossSales>
<TotalNetSales>21.25</TotalNetSales>
<Quantity>1</Quantity>
<SaleType>10</SaleType>
<RevenueCentre>123</RevenueCentre>
<TotalVAT>1.25</TotalVAT>
</SalesTransaction>
<SalesTransaction>
<PLU>64356</PLU>
<Description>Sample Something 1</Description>
<GrossSales>4.1500</GrossSales>
<TotalNetSales>21.25</TotalNetSales>
<Quantity>1</Quantity>
<SaleType>10</SaleType>
<RevenueCentre>123</RevenueCentre>
<TotalVAT>1.25</TotalVAT>
</SalesTransaction>
<SalesTransaction>
<PLU>67657</PLU>
<Description>Sample Desc 1</Description>
<GrossSales>4.1500</GrossSales>
<TotalNetSales>21.25</TotalNetSales>
<Quantity>1</Quantity>
<SaleType>10</SaleType>
<RevenueCentre>123</RevenueCentre>
<TotalVAT>1.25</TotalVAT>
</SalesTransaction>
</SalesHeader>
</Header>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And I would use a key, this is a lot more performant in order to find the nodes
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="trans" match="SalesTransaction/*" use="name()"/>
<xsl:template match="/">
<html>
<body>
<h2>Test Result</h2>
<table border="1" cellpadding="3">
<tbody>
<tr>
<xsl:if test="Header//SalesHeader/RevenueCentre">
<td>RevenueCentre</td>
</xsl:if>
<td>SalesDate</td>
<td>Location</td>
<td>PLU</td>
<xsl:if test="key('trans' , 'CategoryCode')">
<td>CategoryCode</td>
</xsl:if>
<td>Quantity</td>
<td>Description</td>
<xsl:if test="key('trans' , 'SaleType')">
<td>SaleType</td>
</xsl:if>
<xsl:if test="key('trans' , 'GrossSalesPrice')">
<td>GrossSalesPrice</td>
</xsl:if>
<xsl:if test="key('trans' , 'NetSalesPrice')">
<td>NetSalesPrice</td>
</xsl:if>
<xsl:if test="key('trans' , 'TotalGrossSales')">
<td>TotalGrossSales</td>
</xsl:if>
<xsl:if test="key('trans' , 'TotalNetSales')">
<td>TotalNetSales</td>
</xsl:if>
<xsl:if test="key('trans' , 'TotalVAT')">
<td>TotalVAT</td>
</xsl:if>
</tr>
<xsl:apply-templates select=".//SalesTransaction" />
</tbody>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="SalesTransaction">
<tr>
<xsl:apply-templates select="ancestor::SalesHeader" />
<td>
<xsl:value-of select="PLU" />
</td>
<xsl:if test="count(CategoryCode) > 0">
<td>
<xsl:value-of select="CategoryCode" />
</td>
</xsl:if>
<td>
<xsl:value-of select="Quantity" />
</td>
<td>
<xsl:value-of select="Description" />
</td>
<xsl:if test="count(SaleType) > 0">
<td>
<xsl:value-of select="SaleType" />
</td>
</xsl:if>
<xsl:if test="count(GrossSalesPrice) > 0">
<td>
<xsl:value-of select="GrossSalesPrice" />
</td>
</xsl:if>
<xsl:if test="count(NetSalesPrice) > 0">
<td>
<xsl:value-of select="NetSalesPrice" />
</td>
</xsl:if>
<xsl:if test="count(TotalGrossSales) > 0">
<td>
<xsl:value-of select="TotalGrossSales" />
</td>
</xsl:if>
<xsl:if test="count(TotalNetSales) > 0">
<td>
<xsl:value-of select="TotalNetSales" />
</td>
</xsl:if>
<xsl:if test="count(TotalVAT) > 0">
<td>
<xsl:value-of select="TotalVAT" />
</td>
</xsl:if>
</tr>
</xsl:template>
<xsl:template match="SalesHeader">
<xsl:if test="count(RevenueCentre) > 0">
<td>
<xsl:value-of select="RevenueCentre" />
</td>
</xsl:if>
<td>
<xsl:value-of select="SalesDate" />
</td>
<td>
<xsl:value-of select="Location" />
</td>
</xsl:template>
</xsl:stylesheet>
ASKER
He's done it again!
ASKER
sorry - (s)he's done it again!
you can safely say "he" :-)
welcome
welcome
ASKER
Open in new window