Комплексное преобразование HTML-таблицы в XML с помощью xslt

0

Я застрял в создании двух вариантов преобразования html-таблицы в 2 типа xml. Мне нужно было сделать xslt для преобразования таблицы Html, как это

<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>

В xml, как это

<?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>

Я попытался сделать так:

    <?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>

У меня был ответ на первое условие. И именно в Convert html table в xml с помощью xslt

Теги:
html-table
xslt

1 ответ

1
Лучший ответ

попробуйте следующую таблицу стилей

<?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>

Ещё вопросы

Сообщество Overcoder
Наверх
Меню