iain_1976 iain_1976 - 1 year ago 96
HTML Question

XSL using multiple node attributes

I am new to all this and have tried various routes and variations to get the data into my table but I am unable to get the second and third dimension node attribute. I have obviously got something wrong in either the attribute value selection or the template use as I just get a repeat of the first attribute for each column.

My XML input is:

<nodes>
<node name="Server Dashboard">
<children>
<node name="Server Dashboard">
<dimension name="Performance" status="20" id="10" >null</dimension>
<dimension name="System" status="10" id="20" >null</dimension>
<dimension name="Availability" status="30" id="30" >null</dimension>
<children>
<node name="SERVER 1">
<dimension name="Performance" status="20" id="10" >null</dimension>
<dimension name="System" status="10" id="20" >null</dimension>
<dimension name="Availability" status="30" id="30" >null</dimension>
<children>
</children>
</node>
<node name="SERVER 2">
<dimension name="Performance" status="20" id="10" >null</dimension>
<dimension name="System" status="10" id="20" >null</dimension>
<dimension name="Availability" status="30" id="30" >null</dimension>
<children>
</children>
</node>
</children>
</node>
</children>
</node>
</nodes>


And I am trying to get an output like

<html>
<body>
<table border="1">
<th>System</th>
<th>Performance</th>
<th>Status</th>
<th>Availability</th>
</tr>
<tr>
<td>SERVER 1</td>
<td>20</td>
<td>10</td>
<td>30</td>
</tr>
<tr>
<td>SERVER 2</td>
<td>20</td>
<td>10</td>
<td>30</td>
</tr>
</table>
</body>
</html>


And currently I have the xsl of which almost gets me there but not quite as it seems not to loop on the dimension nodes.

<xsl:template match="/">
<html>
<body>
<table border="1">
<th>System</th>
<th>Performance</th>
<th>Status</th>
<th>Availability</th>
</tr>
<xsl:for-each select="nodes/node/children/node/children/node">
<tr>
<td><xsl:value-of select="@name"/></td>
<td><xsl:value-of select="dimension/@Status[//@id='10']"/></td>
<td><xsl:value-of select="dimension/@status[//@id='20']"/></td>
<td><xsl:value-of select="dimension/@status[//@id='30']"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>


My final aim is to replace the status number with a coloured cell or .gif, but baby steps at the moment.
Any help gratefully received.

Answer Source

Try the following. Main point is to select the dimension by id attribute value first, then select the status attribute value.

<xsl:template match="/">
    <html>
        <body>
            <table border="1">
            <tr> 
                <th>System</th>
                <th>Performance</th>
                <th>Status</th>
                <th>Availability</th>
            </tr>
            <xsl:for-each select="nodes/node/children/node/children/node"> 
            <tr> 
                <td><xsl:value-of select="@name"/></td>
                <td><xsl:value-of select="dimension[@id='10']/@status"/></td>   
                <td><xsl:value-of select="dimension[@id='20']/@status"/></td>
                <td><xsl:value-of select="dimension[@id='30']/@status"/></td>
            </tr>
            </xsl:for-each>
            </table>
        </body>
      </html>
</xsl:template>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download