使用PHP将表转换为XML

时间:2016-09-24 20:11:40

标签: php xml html-table

基本上,我一直在尝试将HTML表转换为XML(使用PHP)。

我没有任何正常运行的代码。它应该来自:

<table>
                    <thead>
                        <tr>
                          <th align="left">label1</th>
                          <th align="left">label2</th>
                          <th align="left">label3</th>
                          <th align="left">label4</th>       
                        </tr>
                    </thead>
                    <tbody>         
                        <tr>
                            <td>DATA1</td>
                            <td>DATA2</td>
                            <td>DATA3</td>
                            <td>DATA4</td>
                        </tr>
                     </tbody>
</table>

如下所示:

<root>
    <0>
        <label1>DATA1</label1>
        <label2>DATA2</label2>
        <label3>DATA3</label3>
        <label4>DATA4</label4>
    </0>
</root>

我无法找到任何功能或代码来执行此操作。

1 个答案:

答案 0 :(得分:2)

您通常会使用PHP中提供的XSLTProcessor功能来解决您的问题。特定的转换是使用XSL文件完成的。

这是您要求的特殊情况

请注意,我使用&lt; R0&gt; ......&lt; R1&gt;,&lt; 0&gt; &LT 1为卤素;因为它不是XML元素的有效标记名称。

xml-trans.php(要执行的CLI文件)

<?php

$xslDoc = new DOMDocument();
$xslDoc->load('thexhtml.xsl');

$xmlDoc = new DOMDocument();
$xmlDoc->load('thexhtml.html');

$proc = new XSLTProcessor();
$proc->importStylesheet($xslDoc);
echo $proc->transformToXML($xmlDoc);

?>

thexhtml.xsl

<?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" version="1.0" encoding="UTF-8" indent="yes"/>

  <xsl:template match="/">
    <root>
      <xsl:for-each select="table/tbody/tr">
        <xsl:variable name="rowNum" select="position()-1" />
        <xsl:element name="R{$rowNum}" >
          <xsl:for-each select="td">
            <xsl:variable name="colNum" select="position()" />
            <xsl:variable name="header" select="/table/thead/tr/th[position()=$colNum]"/>
            <xsl:element name="{$header}" >
              <xsl:value-of select="text()" />
            </xsl:element>
          </xsl:for-each>
        </xsl:element>
      </xsl:for-each>
    </root>
  </xsl:template>

</xsl:stylesheet>

thexhtml.html(在表格中有更多行供测试。)

<table>
  <thead>
    <tr>
      <th align="left">label1</th>
      <th align="left">label2</th>
      <th align="left">label3</th>
      <th align="left">label4</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>DATA11</td>
      <td>DATA12</td>
      <td>DATA13</td>
      <td>DATA14</td>
    </tr>
    <tr>
      <td>DATA21</td>
      <td>DATA22</td>
      <td>DATA23</td>
      <td>DATA24</td>
    </tr>
    <tr>
      <td>DATA31</td>
      <td>DATA32</td>
      <td>DATA33</td>
      <td>DATA34</td>
    </tr>
  </tbody>
</table>

我的测试结果

php xsl-trans.php > theresult.xml

theresult.xml

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <R0>
    <label1>DATA11</label1>
    <label2>DATA12</label2>
    <label3>DATA13</label3>
    <label4>DATA14</label4>
  </R0>
  <R1>
    <label1>DATA21</label1>
    <label2>DATA22</label2>
    <label3>DATA23</label3>
    <label4>DATA24</label4>
  </R1>
  <R2>
    <label1>DATA31</label1>
    <label2>DATA32</label2>
    <label3>DATA33</label3>
    <label4>DATA34</label4>
  </R2>
</root>

我希望它有所帮助