微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 如何将XML转换为HTML表?

参见英文答案 > How do you parse and process HTML/XML in PHP?                                    30个
我该怎么做(最好使用PHP):

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

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

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

请注意,列出了所有节点,但只写入了具有值的值.这就像将XML转换为完整的“平面”表,如果你理解我的意思.

解决方法:

如果您不需要操作数据而只需要显示数据,则可以生成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>

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

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

这是在Web浏览器中呈现的内容

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。