码迷,mamicode.com
首页 > Web开发 > 详细

php将xml文件转换为html

时间:2018-03-06 15:37:22      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:php将xml文件转换为html

  • test.xml:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <catalog>
    <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
    </cd>
    </catalog>
  • test.xsl:
    
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  • <xsl:template match="/">
    <html>
    <body>
    <h2>My CD Collection</h2>
    <table border="1">
    <tr bgcolor="#9acd32">
    <th align="left">Title</th>
    <th align="left">Artist</th>
    </tr>
    <xsl:for-each select="catalog/cd">
    <tr>
    <td><xsl:value-of select="title" /></td>
    <td><xsl:value-of select="artist" /></td>
    </tr>
    </xsl:for-each>
    </table>
    </body>
    </html>
    </xsl:template>

    </xsl:stylesheet>

    3. test.php:

    <?php
    $xslDoc = new DOMDocument();
    $xslDoc->load("test.xsl");

    $xmlDoc = new DOMDocument();
    $xmlDoc->load("test.xml");

    $proc = new XSLTProcessor();
    $proc->importStylesheet($xslDoc);
    echo $proc->transformToXML($xmlDoc);

    4. 输出:

    <html>

    <body>
    <h2>My CD Collection</h2>
    <table border="1">
    <tr bgcolor="#9acd32">
    <th align="left">Title</th>
    <th align="left">Artist</th></tr>
    <tr>
    <td>Empire Burlesque</td>
    <td>Bob Dylan</td></tr>
    </table>
    </body>

    </html>

    php将xml文件转换为html

    标签:php将xml文件转换为html

    原文地址:http://blog.51cto.com/12173069/2083453

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    登录后才能评论!
    © 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
    迷上了代码!