<x:transform>-Tag

JSP 标准标签库JSP-Standard-Tag-Bibliothek

Das <x:transform>-Tag wendet XSL auf ein XML-Dokument an.

Syntaxformat

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

Attribute

Das <x:transform>-Tag hat die folgenden Attribute:

Attribute Beschreibung Erforderlich Standardwert
doc Quell-XML-Dokument nein Body
docSystemId URI des Quell-XML-Dokuments nein Keine
xslt XSLT-Stylesheet Ja Keine
xsltSystemId URI des XSLT-Quelldokuments nein Keine
result Objekt zum Empfangen des Transformationsergebnisses nein Auf der Seite ausgeben
var Variable, die das zu transformierende XML-Dokument darstellt nein Auf der Seite ausgeben
scope Gültigkeitsbereich des var-Attributs nein Keine


Beispiel-Demonstration

style.xsl-Datei:

<?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>

Der Code der Datei main.jsp lautet wie folgt:

<%@ 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>

Die Ausgabe sieht wie folgt aus:


JSP 标准标签库JSP-Standard-Tag-Bibliothek

Weitere Erweiterungen