XSLT Show all values of elements

Viewed 83

I have an XML file which contains a list of manufacturers, who are recognised by MembershipNo (primary key) and TokenNo (foreign key). Each Manufacturer produces 4 products (ID as primary key). I'm trying to make an XSLT file for this XML:

<Manufacturers>
<Manufacturer>
    <CompanyName>Axxon</CompanyName>
    <MembershipNo>1075628126</MembershipNo>
    <Token>
        <TokenNo>1</TokenNo>
    </Token>
    <Address>20 Lynagh Crt, Arana Hills, QLD 4054</Address>
    <Contact>61889800081</Contact>
    <Products>
        <Product>
            <ID>12345678</ID>
            <Amount>100</Amount>
            <Weight>50</Weight>
            <Pricing>150</Pricing>
            <Restriction>true</Restriction>
            <MoneyBackGuarantee>true</MoneyBackGuarantee>
            <DCC>150</DCC>
        </Product>
        <Product>
            <ID>215800</ID>
            <Amount>110</Amount>
            <Weight>102</Weight>
            <Pricing>16</Pricing>
            <Restriction>true</Restriction>
            <MoneyBackGuarantee>true</MoneyBackGuarantee>
            <DCC>8572</DCC>
        </Product>
        <Product>
            <ID>726360</ID>
            <Amount>234</Amount>
            <Weight>801</Weight>
            <Pricing>95</Pricing>
            <Restriction>false</Restriction>
            <MoneyBackGuarantee>false</MoneyBackGuarantee>
            <DCC>0</DCC>
        </Product>
        <Product>
            <ID>749742</ID>
            <Amount>148</Amount>
            <Weight>308</Weight>
            <Pricing>21</Pricing>
            <Restriction>false</Restriction>
            <MoneyBackGuarantee>true</MoneyBackGuarantee>
            <DCC>4108</DCC>
        </Product>
    </Products>
</Manufacturer>
<Manufacturer>
    <CompanyName>Neuron Mobile</CompanyName>
    <MembershipNo>3116608681</MembershipNo>
    <Token>
        <TokenNo>2</TokenNo>
    </Token>
    <Address>10 Short St, Carisbrook, VIC 3464</Address>
    <Contact>61887693560</Contact>
    <Products>
        <Product>
            <ID>110053</ID>
            <Amount>175</Amount>
            <Weight>147</Weight>
            <Pricing>51</Pricing>
            <Restriction>false</Restriction>
            <MoneyBackGuarantee>false</MoneyBackGuarantee>
            <DCC>9014</DCC>
        </Product>
        <Product>
            <ID>346779</ID>
            <Amount>881</Amount>
            <Weight>137</Weight>
            <Pricing>35</Pricing>
            <Restriction>true</Restriction>
            <MoneyBackGuarantee>true</MoneyBackGuarantee>
            <DCC>0</DCC>
        </Product>
        <Product>
            <ID>244630</ID>
            <Amount>365</Amount>
            <Weight>780</Weight>
            <Pricing>74</Pricing>
            <Restriction>false</Restriction>
            <MoneyBackGuarantee>false</MoneyBackGuarantee>
            <DCC>5312</DCC>
        </Product>
        <Product>
            <ID>996380</ID>
            <Amount>793</Amount>
            <Weight>286</Weight>
            <Pricing>21</Pricing>
            <Restriction>false</Restriction>
            <MoneyBackGuarantee>true</MoneyBackGuarantee>
            <DCC>3744</DCC>
        </Product>
    </Products>
</Manufacturer>

I want the XSL file to create 3 html tables but when using the following XSL, it shows no data in MembershipNo:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
    <body style="font-family:Arial;font-size:12pt;background-color:#ffff">
        <h1>Manufacturers</h1>
        <table border="1">
            <tr bgcolor="#3399ff">
                <th>Manufacturer</th>
                <th>MembershipNo</th>
                <th>Token</th>
                <th>Address</th>
                <th>Contact</th>
            </tr>
            <xsl:for-each select="Manufacturers/Manufacturer">
                <tr>
                    <td><xsl:value-of select="CompanyName"/></td>
                    <td><xsl:value-of select="MembershipNo"/></td>
                    <td><xsl:value-of select="Token/TokenNo"/></td>
                    <td><xsl:value-of select="Address"/></td>
                    <td><xsl:value-of select="Contact"/></td>
                </tr>
            </xsl:for-each> 
        </table>
        <h1>Products</h1>
        <table border="1">
            <tr bgcolor="#3399ff">
                <th>ID</th>
                <th>Amount</th>
                <th>Weight</th>
                <th>Pricing</th>
                <th>Restriction</th>
                <th>MoneyBackGuarantee</th>
                <th>DCC</th>
            </tr>
            <xsl:for-each select="Manufacturers/Manufacturer/Products/Product">
                <tr>
                    <td><xsl:value-of select="ID"/></td>
                    <td><xsl:value-of select="Amount"/></td>
                    <td><xsl:value-of select="Weight"/></td>
                    <td><xsl:value-of select="Pricing"/></td>
                    <td><xsl:value-of select="Restriction"/></td>
                    <td><xsl:value-of select="MoneyBackGuarantee"/></td>
                    <td><xsl:value-of select="DCC"/></td>
                </tr>
            </xsl:for-each> 
        </table>
        <table border="1">
        <tr bgcolor="#3399ff">
                <th>MembershipNo</th>
                <th>ID</th>
            </tr>
            <xsl:for-each select="/Manufacturers/Manufacturer/Products/Product">
                <tr>
                    <td><xsl:value-of select="div/MembershipNo"/></td>
                    <td><xsl:value-of select="ID"/></td>
                </tr>
            </xsl:for-each> 
        </table>
    </body>
</html>
</xsl:template>
</xsl:stylesheet>
1 Answers

Your xslt code block to build the 3rd table has the products of a manufacturer as active node-set.

<xsl:for-each select="/Manufacturers/Manufacturer/Products/Product">
    <tr>
        <td><xsl:value-of select="div/MembershipNo"/></td>
        <td><xsl:value-of select="ID"/></td>
    </tr>
</xsl:for-each> 

To show the MembershipNo of the manufacturer, you have to go 2 levels upwards.

So instead of using <xsl:value-of select="div/MembershipNo"/>
it has to be <xsl:value-of select="../../MembershipNo" />


Full example
<xsl:for-each select="/Manufacturers/Manufacturer/Products/Product">
    <tr>
        <td><xsl:value-of select="../../MembershipNo" /></td>
        <td><xsl:value-of select="ID"/></td>
    </tr>
</xsl:for-each> 
Related