简体   繁体   English

使用XSLT将XML转换为HTML

[英]XML Transformation to HTML using XSLT

I have simple xml as below 我有如下简单的xml

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

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

I want it's output in HTML table as below. 我希望它在HTML表中输出如下。 (HTML table will have 2 columns headers "Name" and "Address", and i need it's values in rows) I do not want to hardcode "Name" and "Address" headers. (HTML表将具有2列标题“ Name”和“ Address”,我需要它的行值)我不想对“ Name”和“ Address”标题进行硬编码。 They may change in future. 它们将来可能会改变。

Name            Address
A               Address1
B               Address2

Can you please let me know what will be the XSLT for this? 您能否让我知道用于此的XSLT?

Use: 采用:

<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>

Output: 输出:

<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