Skip to content Skip to sidebar Skip to footer

Html Table Header From Xml Element

I have a sample XML and I wanted to convert it to HTML Table but the table header should come from XML elements , example: Have a look at attached xml and xsl, something I am miss

Solution 1:

For you header cells, instead of doing as currently

<thcolspan="{count(/root/Stat/*)}"><xsl:iftest="Configuration[*]"><xsl:attributename="rowspan">2</xsl:attribute></xsl:if><xsl:text>Stat</xsl:text></th>

You need to adjust this to only check the first Stat and Configuration cells

<thcolspan="{count(/root/Device[1]/Stat/*)}"><xsl:iftest="/root/Device[1]/Configuration[*]"><xsl:attributename="rowspan">2</xsl:attribute></xsl:if><xsl:text>Stat</xsl:text></th>

This assumes all the Stat and Configuration elements have the same structure. (Otherwise it become much harder)

For the rows, currently it does

<tr><xsl:apply-templatesselect="Stat/*"mode="row" /><xsl:apply-templatesselect="Configuration/*/*"mode="row" /></tr>

Instead, replace it with a line to select individual Device elements, which correspond to your rows:

<xsl:apply-templates select="Device" />

And then it is in the template that matches Device where you output the row data

<xsl:templatematch="Device"><tr><xsl:apply-templatesselect="Stat/*"mode="row" /><xsl:apply-templatesselect="Configuration/*/*"mode="row" /></tr></xsl:template>

Try this XSLT

<xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:outputomit-xml-declaration="yes"indent="yes" /><xsl:templatematch="/"><tableborder="1"><xsl:apply-templatesname="root" /></table></xsl:template><xsl:templatematch="root"><tr><thcolspan="{count(Device[1]/Stat/*)}"><xsl:iftest="Device[1]/Configuration[*]"><xsl:attributename="rowspan">2</xsl:attribute></xsl:if><xsl:text>Stat</xsl:text></th><xsl:iftest="Device[1]/Configuration[*]"><thcolspan="{count(Device[1]/Configuration/*/*)}"><xsl:text>Configuration</xsl:text></th></xsl:if></tr><tr><xsl:for-eachselect="Device[1]/Configuration/*"><thcolspan="{count(*)}"><xsl:value-ofselect="local-name()" /></th></xsl:for-each></tr><tr><xsl:apply-templatesselect="Device[1]/Stat/*"mode="header" /><xsl:apply-templatesselect="Device[1]/Configuration/*/*"mode="header" /></tr><xsl:apply-templatesselect="Device" /></xsl:template><xsl:templatematch="*"mode="header"><th><xsl:value-ofselect="local-name()" /></th></xsl:template><xsl:templatematch="Device"><tr><xsl:apply-templatesselect="Stat/*"mode="row" /><xsl:apply-templatesselect="Configuration/*/*"mode="row" /></tr></xsl:template><xsl:templatematch="*"mode="row"><td><xsl:value-ofselect="." /></td></xsl:template></xsl:stylesheet>

When applied to your XML, the following is output

<tableborder="1"><tr><thcolspan="5"rowspan="2">Stat</th><thcolspan="10">Configuration</th></tr><tr><thcolspan="5">Up</th><thcolspan="5">Down</th></tr><tr><th>Name</th><th>Hardware</th><th>Software</th><th>Version</th><th>State</th><th>Option1</th><th>Option2</th><th>Option3</th><th>Option4</th><th>Option5</th><th>Option1</th><th>Option2</th><th>Option3</th><th>Option4</th><th>Option5</th></tr><tr><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td><td>2000</td><td>2500000</td><td>0</td><td>0</td><td>NA</td><td>2000</td><td>2500000</td><td>0</td><td>0</td><td>NA</td></tr><tr><td>e</td><td>f</td><td>g</td><td>h</td><td>i</td><td>2000</td><td>2500000</td><td>0</td><td>0</td><td>NA</td><td>2000</td><td>2500000</td><td>0</td><td>0</td><td>NA</td></tr></table>

Post a Comment for "Html Table Header From Xml Element"