簡體   English   中英

使用XSLT將XML轉換為HTML

[英]XML Transformation to HTML using XSLT

我有如下簡單的xml

<Scores>
    <Score1>
       <Name>A</Name>
       <Address>Address1</Address>
    </Score1>

    <Score2>
       <Name>B</Name>
       <Address>Address2</Address>
    </Score2>
</Scores>

我希望它在HTML表中輸出如下。 (HTML表將具有2列標題“ Name”和“ Address”,我需要它的行值)我不想對“ Name”和“ Address”標題進行硬編碼。 它們將來可能會改變。

Name            Address
A               Address1
B               Address2

您能否讓我知道用於此的XSLT?

采用:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/Scores">
        <table>
            <tr>
                <xsl:for-each select="*[1]/*">
                    <th>
                        <xsl:value-of select="local-name()"/>
                    </th>
                </xsl:for-each>
            </tr>
            <xsl:apply-templates select="*"/>
        </table>
    </xsl:template>

    <xsl:template match="*">
        <tr>
            <xsl:for-each select="*">
                <td>
                    <xsl:value-of select="."/>
                </td>
            </xsl:for-each>
        </tr>
    </xsl:template>

</xsl:stylesheet>

輸出:

<table>
  <tr>
    <th>Name</th>
    <th>Address</th>
  </tr>
  <tr>
    <td>A</td>
    <td>Address1</td>
  </tr>
  <tr>
    <td>B</td>
    <td>Address2</td>
  </tr>
</table>

暫無
暫無

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

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