使用xslt将html表转换为xml [英] Convert html table into xml using xslt

查看:141
本文介绍了使用xslt将html表转换为xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在制作将html表转换为2种xml的两个选项时遇到困难。
我需要制作一个xslt来转换这样的Html表

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

<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行作为值,则进入这样的xml文件(意味着从标题中取出类别) id> 1)。

Into xml file like this if the had 1 row as a value(means taking categories out of header where 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作为帮助。这是第一个条件的解决方案

And I got this xsl from Miam84 as a help here. And this is a solution for the first condition

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

And I needed to make a one xsl for two conditions. The otherwise condition is the xml where the had 1 column as a value( need to take categories from where 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源但它并不是真的一个HTML文件,因为你有非html标签(类别,数据集)...所以你的问题出了问题

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

假设你有一个xml源文件你在问题中调用了html,这个xsl可以解决这个问题:

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: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 元素包围:

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

这篇关于使用xslt将html表转换为xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆