【问题标题】:Turn XML data to HTML table with XSLT使用 XSLT 将 XML 数据转换为 HTML 表
【发布时间】:2013-10-31 17:32:05
【问题描述】:

我需要能够将平面 xml 数据集转换为 html 表,但我无法找到适合我需要的语法示例。我想使用一个样式表,可以将外观相似的数据集转换为具有可变列的 html 表。 这是我的 XML 文件的一部分:

<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="XSLT_StyleSheet.xsl"?> <Services>

  <Service WsdlUrl="http://venus.eas.asu.edu/WSRepository/Services/BasicThreeSvc/Service.svc">
    <Name>ServiceName</Name>
    <Provider></Provider>
    <Category>CatName</Category>
    <Operations>
      <Operaion>
        <Name>HelloWorld</Name>
        <MsgIn>elloWorldInputMessage</MsgIn>
        <MsgOut>HelloWorldOutputMessage</MsgOut>
      </Operaion>
      <Operaion>
        <Name>OP2name</Name>
        <MsgIn>InputMessage</MsgIn>
        <MsgOut>OutputMessage</MsgOut>
      </Operaion>
 <Operaion>
        <Name>Op3Name</Name>
        <MsgIn>InputMessage</MsgIn>
        <MsgOut>OutputMessage</MsgOut>
      </Operaion>     
    </Operations>

HTML 表格必须是这样的:

【问题讨论】:

    标签: html xml xslt


    【解决方案1】:

    如果您没有找到使用 XSLT 将 XML 转换为 HTML 的示例,那么您看起来并不难。这是它的主要动机之一。无论如何,这应该让你开始:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
        <xsl:template match="node()|@*"/>
    
        <xsl:template match="/Services">
            <html>
                <head>
                    <title>XSLT example</title>
                </head>
                <body>
                    <xsl:apply-templates/>
                </body>
            </html>
        </xsl:template>
    
        <xsl:template match="Service">
            <xsl:apply-templates/>
        </xsl:template>
    
        <xsl:template match="Operations">
            <table>
                <thead>
                    <tr>
                        <td>Name</td>
                        <td>Description</td>
                        <td>Type</td>
                    </tr>
                </thead>
                <tbody>
                    <xsl:apply-templates/>
                </tbody>
            </table>
        </xsl:template>
    
        <xsl:template match="Operaion"> <!-- [sic] -->
            <xsl:variable name="service" select="ancestor::Service"/>
    
            <tr>
                <td><xsl:value-of select="$service/Name"/></td>
                <td><xsl:value-of select="Name"/></td>
                <td><xsl:value-of select="$service/Category"/></td>
            </tr>
        </xsl:template>
    
    </xsl:transform>
    

    您的(更正后的)文档上的输出(缺少结束标签):

    <html>
        <head>
            <title>XSLT example</title>
        </head>
        <body>
            <table>
                <thead>
                    <tr>
                        <td>Name</td>
                        <td>Description</td>
                        <td>Type</td>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>ServiceName</td>
                        <td>HelloWorld</td>
                        <td>CatName</td>
                    </tr>
                    <tr>
                        <td>ServiceName</td>
                        <td>OP2name</td>
                        <td>CatName</td>
                    </tr>
                    <tr>
                        <td>ServiceName</td>
                        <td>Op3Name</td>
                        <td>CatName</td>
                    </tr>
                </tbody>
            </table>
        </body>
    </html>
    

    【讨论】:

    • 乍一看似乎没问题,但是当我尝试在浏览器中打开 XML 文件时,这两个文件看起来不像表格:dropbox.com/sh/gggkh4wg94simrl/2GbiHAGkVF
    • 正如 TimC 在对this question 的回答中指出的那样,您应该使用 CSS 来设置表格的样式。请将此答案标记为已接受,因为您正在使用该代码(实际上声称它是您自己的)。如果您总是被要求做provide codeaccept answers 之类的基本操作,那么您将耗尽stackoverflow 作为一种资源。
    • 很清楚,但放心,这是真诚的,我还在学习!谢谢你的回答
    猜你喜欢
    • 1970-01-01
    • 2014-03-08
    • 2012-09-29
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 2014-04-22
    • 2013-06-12
    相关资源
    最近更新 更多