用xsl将xml转成html
时间: 2023-10-09 09:06:29 浏览: 47
可以使用XSLT(XSL Transformations)来将XML转换为HTML。以下是一个简单的示例:
XML文件(example.xml):
```xml
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</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>My Book Catalog</h2>
<table border="1">
<tr>
<th>ID</th>
<th>Author</th>
<th>Title</th>
<th>Genre</th>
<th>Price</th>
<th>Publish Date</th>
<th>Description</th>
</tr>
<xsl:for-each select="catalog/book">
<tr>
<td><xsl:value-of select="@id"/></td>
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of select="title"/></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>
```
使用命令行工具或代码解析器将XML和XSL文件传递给XSLT引擎,生成HTML文件:
```html
<html>
<body>
<h2>My Book Catalog</h2>
<table border="1">
<tr>
<th>ID</th>
<th>Author</th>
<th>Title</th>
<th>Genre</th>
<th>Price</th>
<th>Publish Date</th>
<th>Description</th>
</tr>
<tr>
<td>bk101</td>
<td>Gambardella, Matthew</td>
<td>XML Developer's Guide</td>
<td>Computer</td>
<td>44.95</td>
<td>2000-10-01</td>
<td>An in-depth look at creating applications
with XML.</td>
</tr>
<tr>
<td>bk102</td>
<td>Ralls, Kim</td>
<td>Midnight Rain</td>
<td>Fantasy</td>
<td>5.95</td>
<td>2000-12-16</td>
<td>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</td>
</tr>
</table>
</body>
</html>
```
可以使用各种XSLT引擎来执行此转换。例如,如果您有Java环境,则可以使用Saxon XSLT引擎。以下是一个使用Saxon HE的Java代码示例:
```java
import java.io.File;
import java.io.FileOutputStream;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public class XmlToHtml {
public static void main(String[] args) throws Exception {
TransformerFactory factory = TransformerFactory.newInstance();
StreamSource xslStream = new StreamSource(new File("example.xsl"));
Transformer transformer = factory.newTransformer(xslStream);
StreamSource in = new StreamSource(new File("example.xml"));
FileOutputStream out = new FileOutputStream(new File("example.html"));
transformer.transform(in, new StreamResult(out));
System.out.println("HTML file generated.");
}
}
```