0

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

<html>   
  <categories place="row">1</categories>
  <dataset place="head">Characteristics</dataset>
  <table caption="City statistics">
    <thead>
      <tr>
        <th id="1">Name</th> 
        <th id="2">Population</th> 
        <th id="3">Area</th>
        <th id="4">Elevation</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td id="1">Moscow</td> 
        <td id="2">4.7</td> 
        <td id="3">11.54</td> 
        <td id="4">13</td>
      </tr>
      <tr>
        <td id="1">London</td> 
        <td id="2">6</td> 
        <td id="3">15.54</td> 
        <td id="4">15</td>
      </tr>
    </tbody>
  </table>
</html>

Into xml like this

<?xml version="1.0" encoding="UTF-8"?>
<chart caption="City statistics"
       xAxisName="Сharacteristics"
       yAxisName="Values">
   <categories>
      <category label="Moscow"/>
      <category label="London"/>
   </categories>
   <dataset seriesName="Population">
      <set value="4.7"/>
      <set value="6"/>
   </dataset>
   <dataset seriesName="Area">
      <set value="11.54"/>
      <set value="15.54"/>
   </dataset>
   <dataset seriesName="Elevation">
      <set value="13"/>
      <set value="15"/>
   </dataset>
</chart>

What I tried to do is:

    <?xml version='1.0' encoding='utf-8' ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml"/>
    <!-- Default template -->
    <xsl:template match="/">
        <chart caption="" xAxisName="City name" yAxisName="Values">
            <xsl:attribute name="caption">
                <xsl:value-of select="html/table/@caption"/>
            </xsl:attribute>
            <xsl:apply-templates select="html/table/thead"/>
            <xsl:apply-templates select="html/table/tbody"/>
        </chart>
    </xsl:template>
    <!-- template for the thead = categories container -->
    <xsl:template match="thead">
        <dataset>
            <xsl:apply-templates select="tr/th[@id &gt; 1]"/>
        </dataset>
    </xsl:template>
    <!-- template for the th = each category -->
    <xsl:template match="th">
        <dataset>
            <xsl:attribute name="seriesName">
                <xsl:value-of select="."/>
            </xsl:attribute>
            <!--<xsl:apply-templates select="../../../tbody/tr/td[@id = ../../../thead/tr/th/@id]"/>\ -->
            <xsl:for-each select="../../../tbody/tr/td[@id=../../../thead/tr/th/@id]">


            <set value="">
            <xsl:attribute name="value">
                <xsl:value-of select="."/>
            </xsl:attribute>
            </set>
             </xsl:for-each>
        </dataset>
    </xsl:template>
    <!-- template for the thead = categories container -->
    <xsl:template match="tbody">
        <categories>
            <xsl:apply-templates select="tr/td[@id=1]"/>
        </categories>
    </xsl:template>
    <!-- template for the th = each category -->
    <xsl:template match="td">
        <category>
            <xsl:attribute name="label">
                <xsl:value-of select="."/>

            </xsl:attribute>
        </category>
    </xsl:template>
    <!-- <xsl:template match="td">
        <set value="">
            <xsl:attribute name="value">
                <xsl:value-of select="."/>
            </xsl:attribute>
        </set>
    </xsl:template> -->
</xsl:stylesheet>

I had an answer for the first condition. And it is at Convert html table into xml using xslt

1 Answer 1

1

try the following stylesheet

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/">
        <chart caption="{html/table/@caption}" xAxisName="City name" yAxisName="Values">
            <categories>
                <xsl:for-each select="descendant::td[@id='1']">
                    <category label="{.}"/>
                </xsl:for-each>
            </categories>
            <xsl:for-each select="descendant::th[@id &gt; 1]">
                <xsl:variable name="curr_id" select="@id"/>
                <dataset seriesName="{.}">
                    <xsl:for-each select="../../../descendant::td">
                        <xsl:if test="@id=$curr_id">
                            <set value="{.}"/>
                        </xsl:if>
                    </xsl:for-each>
                </dataset>
            </xsl:for-each>

        </chart>
    </xsl:template>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.