如何在 XML 文档上应用 XSL 转换?
<x:transform> 标签将 XSL 转换应用于 XML 文档。
属性
<x:transform> 标签具有以下属性:
属性 | 描述 | 是否必填 | 默认值 |
---|---|---|---|
doc | XSLT 转换的源 XML 文档 | 否 | 主体 |
docSystemId | 原始 XML 文档的 URI | 否 | 无 |
xslt | 提供转换指令的 XSLT 样式表 | 是 | 无 |
xsltSystemId | 原始 XSLT 文档的 URI | 否 | 无 |
result | 接受转换结果的结果对象 | 否 | 打印到页面 |
var | 设置为已转换 XML 文档的变量 | 否 | 打印到页面 |
scope | 公开转换结果的变量的作用域 | 否 | 无 |
示例
考虑以下 XSLT 样式表 style.xsl:
<?xml version = "1.0"?> <xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0"> <xsl:output method = "html" indent = "yes"/> <xsl:template match = "/"> <html> <body> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match = "books"> <table border = "1" width = "100%"> <xsl:for-each select = "book"> <tr> <td> <i><xsl:value-of select = "name"/></i> </td> <td> <xsl:value-of select = "author"/> </td> <td> <xsl:value-of select = "price"/> </td> </tr> </xsl:for-each> </table> </xsl:template> </xsl:stylesheet>
现在考虑以下 JSP 文件:
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix = "x" uri = "http://java.sun.com/jsp/jstl/xml" %> <html> <head> <title>JSTL x:transform Tags</title> </head> <body> <h3>Books Info:</h3> <c:set var = "xmltext"> <books> <book> <name>Padam History</name> <author>ZARA</author> <price>100</price> </book> <book> <name>Great Mistry</name> <author>NUHA</author> <price>2000</price> </book> </books> </c:set> <c:import url = "http://localhost:8080/style.xsl" var = "xslt"/> <x:transform xml = "${xmltext}" xslt = "${xslt}"/> </body> </html>
您将收到以下结果:
书籍信息
Padam 历史 ZARA | 100 |
伟大的谜团 NUHA | 2000 |
广告