Skip to content Skip to sidebar Skip to footer

Convert Html Table Into Xml Using Xslt

I got stuck with making the two options in converting html table into 2 types of xml. I needed to make a xslt to convert Html table like this 1 row

Solution 1:

You say you have an html source but it's not really a HTML file because you have non-html tags in it (category, dataset)... So something is wrong in your question

Assuming you have an xml source file that you called html in your question, this xsl should do the trick :

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheetxmlns:xsl="http://www.w3.org/1999/XSL/Transform"version="1.0"><xsl:outputmethod="xml"/><!-- Default template --><xsl:templatematch="/"><chartcaption=""xAxisName="City name"yAxisName="Values"><xsl:attributename="caption"><xsl:value-ofselect="root/table/@caption" /></xsl:attribute><xsl:apply-templatesselect="root/table/thead"/><xsl:apply-templatesselect="root/table/tbody/tr"/></chart></xsl:template><!-- template for the thead = categories container --><xsl:templatematch="thead"><categories><xsl:apply-templatesselect="tr/th[@id &gt; 1]" /></categories></xsl:template><!-- template for the th = each category --><xsl:templatematch="th"><category><xsl:attributename="label"><xsl:value-ofselect="." /></xsl:attribute></category></xsl:template><!-- template for the tr = the dataset --><xsl:templatematch="tr"><datasetseriesName=""><xsl:attributename="seriesName"><xsl:value-ofselect="td[@id=1]" /></xsl:attribute><xsl:apply-templatesselect="td[@id &gt; 1]" /></dataset></xsl:template><!-- template for the td = the set tag --><xsl:templatematch="td"><setvalue=""><xsl:attributename="value"><xsl:value-ofselect="." /></xsl:attribute></set></xsl:template></xsl:stylesheet>

But you have to know that a well formed xml has a root element, so I took your xml source surrounded by a root element :

<root><categories>1 row</categories><dataset>1 column</dataset><tablecaption="City statistics"><thead><tr><thid="1">Name</th><thid="2">Population</th><thid="3">Area</th><thid="4">Elevation</th></tr></thead><tbody><tr><tdid="1">Moscow</td><tdid="2">4.7</td><tdid="3">11.54</td><tdid="4">13</td></tr><tr><tdid="1">London</td><tdid="2">6</td><tdid="3">15.54</td><tdid="4">15</td></tr></tbody></table></root>

Post a Comment for "Convert Html Table Into Xml Using Xslt"