-1

How do I go from this (using PHP preferably):

<bookstore>
  <book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="web" cover="paperback">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

To this (please imagine this to be an actual HTML table, I am not allowed to post images yet):

|bookstore|book|    title       |     author        |year|price|
|         |    |Everyday Italian|Giada De Laurentiis|2005|30.00|
|         |    |Learning XML    |Erik T. Ray        |2003|39.95|

Please notice that all nodes are listed but only those values are written that have a value. This is like converting the XML into a complete a "flat" table, if you get what I mean.

  • 1
    You'll need to use something like SimpleXml, then figure it out from there. – Frank Apr 13 '15 at 15:04
  • Yeah, but how. I have used every XML parser PHP has. It's about the logic of generating this kind of table. – Asfandyar Hashmi Apr 13 '15 at 15:12
  • IF available XML parsers don't make what you want, your last resort is a bunch of regexp filtering sequentially every line of your file. – Marco Bernardini Apr 13 '15 at 15:14
  • 1
    Iterate through the `` items and construct your own HTML. You don't need regex for this. – Frank Apr 13 '15 at 15:15
  • Also please do not duplicate your own questions. Instead edit and improve the original one. That's [this one](http://stackoverflow.com/q/29465946/367456) which I now closed against the one here. – hakre Apr 13 '15 at 20:53
  • Probably related: [How excel reads XML file?](http://stackoverflow.com/q/25992326/367456) – hakre Apr 13 '15 at 20:58

1 Answers1

1

If you don't need to manipulate the data and just need to present it you can make an XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <table>
      <tr>
        <th>Bookstore</th>
        <th>Book</th>
        <th>title</th>
        <th>author</th>
        <th>year</th>
        <th>price</th>
      </tr>
      <xsl:for-each select="bookstore/book">
      <tr>
         <td></td>
         <td></td>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="author"/></td>
         <td><xsl:value-of select="year"/></td>
        <td><xsl:value-of select="price"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

You can use php XSLT processor to generate the html or just link to the XSLT directly in the xml. For example if you link it like so:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="bookstore.xsl"?>
<bookstore>
  <book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="web" cover="paperback">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

This is what would be rendered in the web browser:

<html>
<body>
    <table>
        <tbody>
            <tr>
                <th>Bookstore</th>
                <th>Book</th>
                <th>title</th>
                <th>author</th>
                <th>year</th>
                <th>price</th>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td>Everyday Italian</td>
                <td>Giada De Laurentiis</td>
                <td>2005</td>
                <td>30.00</td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td>Learning XML</td>
                <td>Erik T. Ray</td>
                <td>2003</td>
                <td>39.95</td>
            </tr>
        </tbody>
    </table>
</body>
</html>
  • I have to input XML and generate the table. I can't hard code the XML. – Asfandyar Hashmi Apr 13 '15 at 15:38
  • @AsfandyarHashmi thats exactly what an XSLT does! It does not replace your xml document but transforms it into a different output. I've added an example to my answer. –  Apr 13 '15 at 15:53
  • But I can't hard code anything. It has to be generic. For example, you used title, author etc. I have to derive that from the XML itself. – Asfandyar Hashmi Apr 13 '15 at 16:00