簡體   English   中英

如何使用 xslt 將 xml 轉換為 html 表

[英]How to transform a xml to html table using xslt

例如,我有一個 xml 文檔:

<?xml version="1.0" encoding="UTF-8"?>
<tests>
<testrun run="test1">
    <test name="foo" pass="true" />
    <test name="bar" pass="true" />
    <test name="baz" pass="true" />
</testrun>
<testrun run="test2">
    <test name="foo" pass="true" />
    <test name="bar" pass="false" />
    <test name="baz" pass="false" />
</testrun>
</tests>

如果我將其轉換為帶有 xsl 文檔的 html,我可能會得到如下輸出:

<html>
<body>test1<table>
    <tr>
        <td>Test</td><td>Pass</td>
    </tr>
    <tr>
        <td>foo</td><td>true</td>
    </tr>
    <tr>
        <td>bar</td><td>true</td>
    </tr>
    <tr>
        <td>baz</td><td>true</td>
    </tr>
</table>test2<table>
    <tr>
        <td>Test</td><td>Pass</td>
    </tr>
    <tr>
        <td>foo</td><td>true</td>
    </tr>
    <tr>
        <td>bar</td><td>false</td>
    </tr>
    <tr>
        <td>baz</td><td>false</td>
    </tr>
</table>
</body>

我的問題如下:
我如何獲得此輸出:

<html>
<body>test1<table>
    <tr>
        <td>Test</td><td>Pass</td>
    </tr>
    <tr>
        <td>foo</td><td>true</td>
    </tr>
    <tr>
        <td>bar</td><td>true</td>
    </tr>
    <tr>
        <td>baz</td><td>true</td>
    </tr>
</table>
</body>

和這個:

<html>
    <body>test2<table>
        <tr>
            <td>Test</td><td>Pass</td
        </tr>
        <tr>
            <td>foo</td><td>true</td>
        </tr>
        <tr>
            <td>bar</td><td>false</td>
        </tr>
        <tr>
            <td>baz</td><td>false</td>
        </tr>
    </table>
    </body>
</html>

我怎樣才能得到它,有人有意見嗎?

如果我正確理解您的問題,以下內容就可以解決問題。 我不確定,你所說的 html 字符串是什么意思。 如果您需要輸出實際的單個 html 文檔,這還不夠:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

<!-- This template is applied for each 'testrun' node in the xml document. It outputs the html, body and table tags and processes all children of the 'testrun' nodes according to any other templates.-->
    <xsl:template match="testrun">
      <hmtl>
       <body>
       <xsl:value-of select="@run"/>
        <table>
            <xsl:apply-templates/>            
        </table>
       </body>
      </hmtl>
    </xsl:template>

<!-- This template prevents that the 'tests' node is output, and makes sure that all child nodes of 'tests' nodes have matching templates applied to them. -->
    <xsl:template match="tests">
        <xsl:apply-templates></xsl:apply-templates>
    </xsl:template>

<!-- This template copies every single node from the xml without doing anything
other than making an exact copy. The other templates override this template.-->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

<!--This template makes sure that a table row with two cells with the values of the 'name' and 'pass' attributes of 'test' nodes are output for each 'test' node.-->
        <xsl:template match="test">
         <tr>
             <td><xsl:value-of select="@name"/></td>
             <td><xsl:value-of select="@pass"/></td>
         </tr>
    </xsl:template>
</xsl:transform>

編輯:根據要求添加了模板的解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM