HTML转换表到使用XML XSLT(Convert html table into xml usi

2019-10-19 13:44发布

我被困在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>

成这样的xml文件,如果有1行作为一个值(装置采取的类别输出接头的其中id> 1)。

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

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


文章来源: Convert html table into xml using xslt