【问题标题】:online xml to html parser在线 xml 到 html 解析器
【发布时间】:2013-10-23 05:46:23
【问题描述】:

我正在寻找将任何 xml 解析为 html 的在线程序。例如,如果我有以下 xml 布局

<users>
    <user>
         </name>john</name>
         <pictures>
              <pic1>URL of picture1.png</pic1>
              <pic2>URL of picture2.png</pic2>
         </pictures>
    </user>
    <user>
         </name>mary</name>
         <pictures>
              <pic1>URL of picture1.png</pic1>
              <pic2>URL of picture2.png</pic2>
         </pictures>
    </user>
</users>

它可以创建一个显示节点内容的 html 页面,并提供一些最小的格式以便于阅读。有这样的工具吗?

编辑:我只是举了一个例子,我想要一个通用工具,可以解析任何 xml 而无需事先知道其结构。

【问题讨论】:

    标签: html xml parsing


    【解决方案1】:

    解决此问题的一种方法是应用 XSLT 转换。

    您需要创建一个 XSL 样式表,例如...

    <?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>
    <head>
        <title>Contacts</title>
    </head>
    <body>
        <xsl:for-each select="//user">
            <h2><xsl:value-of select="name" /></h2>
            <p><xsl:value-of select="pictures/pic1" /></p>
            <p><xsl:value-of select="pictures/pic2" /></p>
        </xsl:for-each>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet> 
    

    然后在您的 xml 文件顶部链接到它。在下面的示例中,第二行链接到名为“myStyleSheet.xsl”的样式表。

    <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type="text/xsl" href="myStyleSheet.xsl"?>
    <users>
        <user>
             <name>john</name>
             <pictures>
                  <pic1>URL of picture1.png</pic1>
                  <pic2>URL of picture2.png</pic2>
             </pictures>
        </user>
        <user>
             <name>mary</name>
             <pictures>
                  <pic1>URL of picture1.png</pic1>
                  <pic2>URL of picture2.png</pic2>
             </pictures>
        </user>
    </users>
    

    【讨论】:

    • 这个解决方案假设我知道xml文件的结构。
    【解决方案2】:

    你可以试试这个。

    http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_choose

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!-- Edited by XMLSpy® -->
    <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>Title</th>
      <th>Artist</th>
    </tr>
    <xsl:for-each select="catalog/cd">
    <tr>
      <td><xsl:value-of select="title"/></td>
      <xsl:choose>
      <xsl:when test="price > 10">
         <td bgcolor="#ff00ff">
         <xsl:value-of select="artist"/>
         </td>
      </xsl:when>
      <xsl:otherwise>
         <td><xsl:value-of select="artist"/></td>
      </xsl:otherwise>
      </xsl:choose>
      </tr>
    </xsl:for-each>
     </table>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

    • 这绝不是通用的,在 OP 给出的示例的情况下,结果与原始数据无关。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 2016-10-12
    • 2012-08-26
    相关资源
    最近更新 更多