【问题标题】:Convert html table into xml using xslt使用 xslt 将 html 表转换为 xml
【发布时间】:2014-03-15 10:03:38
【问题描述】:

在将 html 表转换为 2 种类型的 xml 时,我一直坚持使用这两个选项。 我需要制作一个 xslt 来像这样转换 Html 表

<categories>1 row</categories>
<dataset>1 column</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>

如果有 1 行作为值(意味着从 id > 1 的标题中取出类别),则进入这样的 xml 文件。

 <chart caption='City statistics' xAxisName='City name' yAxisName='Values'>

       <categories>
          <category label='Population' />
          <category label='Area' />
          <category label='Elevation' />
       </categories>

       <dataset seriesName='Moscow'>
          <set value='4.7' />
          <set value='11.54'/>
          <set value='13' />
       </dataset>

       <dataset seriesName='London'>
          <set value='6'/>
          <set value='15.54'/>
          <set value='15'/>
       </dataset>
    </chart>

我从 Miam84 那里得到了这个 xsl 作为帮助。这是第一个条件的解决方案

我需要为两个条件制作一个 xsl。 否则条件是 xml 其中有 1 列作为值(需要从 id=1 获取类别)。

<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="/">
    <xsl:choose>
            <xsl:when test="//categories[@place='head']" >
        <chart caption="" xAxisName="" yAxisName="Values">
            <xsl:attribute name="xAxisName">
                <xsl:value-of select="html/categories"/>
            </xsl:attribute>
            <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/tr"/>
        </chart>
        </xsl:when>
            <xsl:otherwise>
            <chart caption="" xAxisName="" yAxisName="Values">
            <xsl:attribute name="xAxisName">
                <xsl:value-of select="html/dataset"/>
            </xsl:attribute>
            <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/tr"/>
            </chart>
            </xsl:otherwise>    
        </xsl:choose>   
    </xsl:template>
    <!-- template for the thead = categories container -->
    <xsl:template match="tbody">

        <xsl:choose>
            <xsl:when test="//categories[@place='head']" >


            </xsl:when>
            <xsl:otherwise>
                <categories>
                <xsl:apply-templates select="tr/td[@id=1]"/>
                </categories>
            </xsl:otherwise>    
        </xsl:choose>   
    </xsl:template>

    <xsl:template match="thead">

        <xsl:choose>
            <xsl:when test="//categories[@place='head']" >
                <categories>
                <xsl:apply-templates select="tr/th[@id &gt; 1]"/>
                </categories>

            </xsl:when>
            <xsl:otherwise>

            </xsl:otherwise>    
        </xsl:choose>   
    </xsl:template>
    <!-- template for the th = each category -->
    <xsl:template match="th">
    <xsl:choose>    
            <xsl:when test="//categories[@place='head']" >
                <category>
                <xsl:attribute name="label">
                <xsl:value-of select="."/>
                </xsl:attribute>
                </category>
            </xsl:when>
            <xsl:otherwise>
            <dataset seriesName="">
            <xsl:attribute name="seriesName">
                <xsl:value-of select="."/>
            </xsl:attribute>
            </dataset>
            </xsl:otherwise>    
        </xsl:choose>   

    </xsl:template>
    <!-- template for the tr = the dataset -->

    <xsl:template match="tr">
    <xsl:choose>    
            <xsl:when test="//categories[@place='head']" >
                <dataset seriesName="">
            <xsl:attribute name="seriesName">
                <xsl:value-of select="td[@id=1]"/>
            </xsl:attribute>
            <xsl:apply-templates select="td[@id &gt; 1]"/>
        </dataset>
            </xsl:when>

            <xsl:otherwise>
            <dataset seriesName="">
            <xsl:attribute name="seriesName">
                <xsl:value-of select="th[@id &gt; 1]"/>
            </xsl:attribute>
            </dataset>


            </xsl:otherwise>    
        </xsl:choose>   


    </xsl:template>
    <!-- template for the td = the set tag -->
    <xsl:template match="td">
    <xsl:choose>
    <xsl:when test="//categories[@place='head']" >
                <set value="">
            <xsl:attribute name="value">
                <xsl:value-of select="."/>
            </xsl:attribute>
        </set>
            </xsl:when>

            <xsl:otherwise>
                <category>
                <xsl:attribute name="label">
                <xsl:value-of select="."/>
                </xsl:attribute>
                </category>



            </xsl:otherwise>    
        </xsl:choose>   



    </xsl:template>

<xsl:template match="html">

</xsl:template>
</xsl:stylesheet>

【问题讨论】:

    标签: html xml xslt html-table


    【解决方案1】:

    你说你有一个 html 源代码,但它不是一个真正的 HTML 文件,因为你有非 html 标签(类别、数据集)......所以你的问题有问题

    假设你有一个在你的问题中称为 html 的 xml 源文件,这个 xsl 应该可以解决问题:

    <?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="root/table/@caption" />
          </xsl:attribute>
    
          <xsl:apply-templates select="root/table/thead"/>
          <xsl:apply-templates select="root/table/tbody/tr"/>   
        </chart>  
      </xsl:template>
    
    
      <!-- template for the thead = categories container -->
      <xsl:template match="thead">
        <categories>
          <xsl:apply-templates select="tr/th[@id &gt; 1]" />        
        </categories>
      </xsl:template>
    
    
      <!-- template for the th = each category -->
      <xsl:template match="th">
        <category>
          <xsl:attribute name="label">
            <xsl:value-of select="." />
          </xsl:attribute>
        </category>
      </xsl:template>
    
      <!-- template for the tr = the dataset -->
      <xsl:template match="tr">
        <dataset seriesName="">
          <xsl:attribute name="seriesName">
            <xsl:value-of select="td[@id=1]" />
          </xsl:attribute>  
          <xsl:apply-templates select="td[@id &gt; 1]" />       
        </dataset>
      </xsl:template>
    
    
      <!-- template for the td = the set tag -->
      <xsl:template match="td">
        <set value="">
          <xsl:attribute name="value">
            <xsl:value-of select="." />
          </xsl:attribute>
        </set>
      </xsl:template>
    
    </xsl:stylesheet>
    

    但你必须知道格式良好的 xml 有一个根元素,所以我将你的 xml 源代码用root 元素包围:

    <root>   
      <categories>1 row</categories>
      <dataset>1 column</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>
    </root>
    

    【讨论】:

      猜你喜欢
      • 2014-03-08
      • 2012-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-22
      • 2013-06-12
      • 1970-01-01
      相关资源
      最近更新 更多