Latest web development tutorials

<X:変換>タグ

JSP標準タグライブラリ JSP標準タグライブラリ

<X:トランスフォーム>タグは、XML文書にXSLを適用します。

構文

<x:transform
   var="<string>"
   scope="<string>"
   result="<string>"
   doc="<string>"
   docSystemId="<string>"
   xslt="<string>"
   xsltSystemId="<string>"/>

プロパティ

<X:変換>タグには、次の属性があります。

プロパティ 説明 必要に応じて デフォルト
ドキュメント ソースXML文書 いいえ ボディ
docSystemId URIソースXML文書 いいえ いいえ
XSLT XSLTスタイルシート それは いいえ
xsltSystemId URIソースXSLTドキュメント いいえ いいえ
結果 変換結果のオブジェクトを受け取り いいえ ページに印刷
VAR 変換後のXML文書変数の代わりに いいえ ページに印刷
スコープ スコープvar属性 いいえ いいえ


例のデモ

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>

main.jspファイルのコードは次のとおりです。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ 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 标签</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>

結果は以下のとおりであります:


JSP標準タグライブラリ JSP標準タグライブラリ