返回首页 JSP 教程

JSP 基础教程

JSP 高级教程

JSP 面试问题

JSP 有用的资源

JSTL XML < x:transform > 标签

<x:transform> 标签用于 XML 文档中的 XML 转换。

属性:

<x:transform> 标签具有如下所示属性:

属性 描述 是否必需 默认值
doc XSLT 转换的源 XML 文档 Body
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>

这将产生如下所示的结果:

Books Info: