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

6
  • 1
    You'll need to use something like SimpleXml, then figure it out from there. Commented Apr 13, 2015 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. Commented Apr 13, 2015 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. Commented Apr 13, 2015 at 15:14
  • 1
    Iterate through the <book> items and construct your own HTML. You don't need regex for this. Commented Apr 13, 2015 at 15:15
  • Also please do not duplicate your own questions. Instead edit and improve the original one. That's this one which I now closed against the one here. Commented Apr 13, 2015 at 20:53

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

I have to input XML and generate the table. I can't hard code the XML.
@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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.