0

So basically, I've been trying to convert an HTML table into XML (with PHP).

I don't have any functioning code. It should go from:

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

To something like the following:

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

I wasn't able to locate any functions, or code to do this.

0

1 Answer 1

2

What you're asking is usually solved with the XSLTProcessor functionality available in PHP. The particular transformation is done using an XSL file.

Here's the special case that you request

Note that I use <R0> ... <R1>, not <0> <1> as it would not be a valid tag name for an XML element.

xml-trans.php (the CLI file to execute)

<?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 (With some more lines in the table for testing.)

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

My testing result

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>

I hope it helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.