แท็ก <x:transform> ใช้นำ XSLT Stylesheet มา Transform XML Document ให้เป็นรูปแบบอื่น (HTML, Text, XML อื่น)
แอตทริบิวต์ของ <x:transform>
| แอตทริบิวต์ | คำอธิบาย | จำเป็น | ค่าเริ่มต้น |
|---|---|---|---|
| doc | XML Document ต้นทางสำหรับ Transformation | ไม่ | Body |
| docSystemId | URI ของ XML Document ต้นทาง | ไม่ | None |
| xslt | XSLT Stylesheet สำหรับ Transformation | ใช่ | None |
| xsltSystemId | URI ของ XSLT Document ต้นทาง | ไม่ | None |
| result | Result Object รับผลลัพธ์ Transformation | ไม่ | Print to page |
| var | ชื่อตัวแปรเก็บ XML ที่ Transform แล้ว | ไม่ | Print to page |
| scope | ขอบเขตของตัวแปร | ไม่ | None |
ตัวอย่าง: Transform XML เป็น HTML Table
XSLT Stylesheet (style.xsl)
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="https://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 ใช้ x:transform
<%@ taglib prefix="c" uri="https://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="https://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://localhost:8080/style.xsl" var="xslt"/>
<x:transform xml="${xmltext}" xslt="${xslt}"/>
</body>
</html>
ผลลัพธ์
Books Info
| Padam History ZARA |
100 |
| Great Mistry NUHA |
2000 |
ประโยชน์: แยก Presentation Logic (XSLT) ออกจาก Data (XML) และ JSP ได้อย่างชัดเจน