如何将XML转换为HTML表格? [英] How to convert XML to HTML table?

查看:145
本文介绍了如何将XML转换为HTML表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么从这里(最好使用PHP):

 < bookstore> 
< title lang =en>每天意大利语< / title>
< author> Giada De Laurentiis< / author>
< year> 2005< / year>
<价格> 30.00< / price>
< / book>
< book category =webcover =paperback>
< title lang =en>学习XML< / title>
< author> Erik T. Ray< / author>
< year> 2003< / year>
<价格> 39.95< / price>
< / book>
< / bookstore>

对此(请想象这是一张实际的HTML表格,我不允许发布图片):

  |书店|书|标题|作者|年|价格| 
| | |日常意大利语| Giada De Laurentiis | 2005 | 30.00 |
| | |学习XML | Erik T. Ray | 2003 | 39.95 |

请注意,所有节点都已列出,但只有那些值才会写入。这就像将XML转换为完整的平面表,如果你明白我的意思。 解决方案

如果你不喜欢无需操作数据,只需呈现数据即可制作 XSLT

 <?xml version =1.0encoding =UTF-8?> 
< xsl:stylesheet version =1.0xmlns:xsl =http://www.w3.org/1999/XSL/Transform>
< xsl:template match =/>
< html>
< body>
< table>
< tr>
书店< / th>
< th> Book< / th>
< th>标题< / th>
< th>作者< / 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>

您可以使用php XSLT处理器直接在xml中生成html或仅链接到XSLT。例如,如果你像这样链接它:

 <?xml version =1.0encoding =UTF-8? > 
<?xml-stylesheet type =text / xslhref =bookstore.xsl?>
< bookstore>
< title lang =en>每天意大利语< / title>
< author> Giada De Laurentiis< / author>
< year> 2005< / year>
<价格> 30.00< / price>
< / book>
< book category =webcover =paperback>
< title lang =en>学习XML< / title>
< author> Erik T. Ray< / author>
< year> 2003< / year>
<价格> 39.95< / price>
< / book>
< / bookstore>

这是在网络浏览器中显示的内容:

 < html> 
< body>
< table>
< tbody>
< tr>
书店< / th>
< th> Book< / th>
< th>标题< / th>
< th>作者< / 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>学习XML< / td>
< td> Erik T. Ray< / td>
< td> 2003< / td>
< td> 39.95< / td>
< / tr>
< / tbody>
< / table>
< / body>
< / html>


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.

解决方案

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>

这篇关于如何将XML转换为HTML表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆