用xsl将xml转成html
时间: 2023-10-09 22:06:29 浏览: 66
可以使用XSLT(XSL Transformations)来将XML转换为HTML,以下是一个示例:
XML文件(example.xml):
```xml
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="001">
<author>John Doe</author>
<title>XML for Dummies</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-12-16</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="002">
<author>Jane Smith</author>
<title>HTML for Dummies</title>
<genre>Computer</genre>
<price>39.95</price>
<publish_date>2001-12-01</publish_date>
<description>Learn HTML quickly and easily.</description>
</book>
</catalog>
```
XSL文件(example.xsl):
```xml
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Book Catalog</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Title</th>
<th style="text-align:left">Author</th>
<th style="text-align:left">Genre</th>
<th style="text-align:left">Price</th>
<th style="text-align:left">Publish Date</th>
<th style="text-align:left">Description</th>
</tr>
<xsl:for-each select="catalog/book">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of select="genre"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="publish_date"/></td>
<td><xsl:value-of select="description"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
```
在浏览器中打开HTML文件,并将XML和XSL文件作为参数传递给transformNode方法:
```html
<!DOCTYPE html>
<html>
<head>
<title>XML to HTML using XSLT</title>
</head>
<body>
<script>
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.load("example.xml");
var xslDoc = new ActiveXObject("Microsoft.XMLDOM");
xslDoc.async = false;
xslDoc.load("example.xsl");
document.write(xmlDoc.transformNode(xslDoc));
</script>
</body>
</html>
```
运行HTML文件,将会显示一个带有XML数据的HTML表格。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)