我有一个如下所示的 XML 文件:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<mylist name="test">
<title>--$title$- not found</title>
<table>
<header>
<col name="firstname" title="--$firstname$- not found"/>
<col name="lastname" title="--$lastname$- not found"/>
<col name="country" title="--$country$- not found"/>
</header>
<body>
<row>
<col name="firstname">John</col>
<col name="lastname">Smith</col>
<col name="country">ENGLAND</col>
</row>
<row>
<col name="firstname">Peter</col>
<col name="lastname">Scott</col>
<col name="country">USA</col>
</row>
</body>
</table>
</mylist>
我需要将结果显示为如下所示的 HTML 表格:
有人可以帮忙吗?我不喜欢 XML/XSL,我只需要一次
最佳答案
试试这个:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="mylist">
<html>
<xsl:apply-templates />
</html>
</xsl:template>
<xsl:template match="title">
<head>
<title><xsl:value-of select="." /></title>
</head>
</xsl:template>
<xsl:template match="table">
<table style="border: 1px solid black">
<xsl:apply-templates />
</table>
</xsl:template>
<xsl:template match="header">
<thead>
<tr>
<xsl:apply-templates />
</tr>
</thead>
</xsl:template>
<xsl:template match="body">
<tbody>
<xsl:apply-templates />
</tbody>
</xsl:template>
<xsl:template match="row">
<tr>
<xsl:apply-templates />
</tr>
</xsl:template>
<xsl:template match="col[parent::header]">
<th style="border: solid black 1px;"><xsl:value-of select="@name" /></th>
</xsl:template>
<xsl:template match="col[parent::row]">
<td style="border: solid black 1px;"><xsl:value-of select="." /></td>
</xsl:template>
</xsl:stylesheet>
它实际上会在每个 <td>
上放置一个样式属性或 <th>
这意味着输出有点冗长,但它使 XSLT 保持美观和简单。
关于html - 简单的 XML 到 HTML 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6650230/