如何在 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 = "https://:8080/style.xsl" var = "xslt"/>
<x:transform xml = "${xmltext}" xslt = "${xslt}"/>
</body>
</html>您將收到以下結果:
書籍資訊
| Padam 歷史 ZARA | 100 |
| 偉大的謎團 NUHA | 2000 |
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP