How to convert XML to HTML table? [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:23:02

问题:

This question already has an answer here:

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:

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> 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!