Java Tutorial/JSTL/XML

Материал из Java эксперт
Перейти к: навигация, поиск

Import XML Data using JSTL

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.ru/jstl/xml" prefix="x" %>
<c:import var="xml" url="students.xml" />
<c:import var="xslt" url="transform.xsl" />
<x:transform xml="${xml}" xslt="${xslt}" />





Output with/without Encode

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Out with Tag Escaping Examples</title>
  </head>
  <body>
    <c:set var="test" scope="page">
      <table border="0">
        <tr>
          <td bgcolor="red">&#160;</td>
          <td bgcolor="green">&#160;</td>
        </tr>
        <tr>
          <td bgcolor="blue">&#160;</td>
          <td bgcolor="yellow">&#160;</td>
        </tr>
      </table>
    </c:set>
    <h3>Out With Encode=true</h3>
    <c:out value="${test}" escapeXml="true" />
    <br />
    <h3>Out With Encode=false</h3>
    <c:out value="${test}" escapeXml="false" />
    <br />
  </body>
</html>





Set XML Data in JSP Page with JSTL

<%@taglib prefix="c" uri="http://java.sun.ru/jstl/core" %>
<%@taglib prefix="x" uri="http://java.sun.ru/jstl/xml" %>
<c:set var="xml">
  <paragraph>
     This document uses <bold>unusual</bold> markup,
     which we want to replace with <bold>HTML</bold>.
  </paragraph>
</c:set>
<c:set var="xsl">
  <?xml version="1.0"?>
  <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
  <xsl:template match="paragraph">
    <P><xsl:apply-templates/></p>
  </xsl:template>
  <xsl:template match="bold">
    <b><xsl:value-of select="."/></b>
  </xsl:template>
  </xsl:stylesheet>
</c:set>
<x:transform xml="${xml}" xslt="${xsl}"/>





Use Table to Display XML Data by JSTL

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.ru/jstl/xml" prefix="x" %>
<html>
  <head>
    <title>For Each Examples</title>
  </head>
  <body>
    <c:import var="students" url="students.xml" />
    <x:parse var="doc" xml="${students}" />
    <table border="1">
      <tr>
        <TH>First</th>
        <TH>Last</th>
        <TH>Points</th>
        <TH>Letter</th>
      </tr>
      <x:forEach var="student" select="$doc/students/student">
        <tr>
          <td>
            <x:out select="name/first" />
          </td>
          <td>
            <x:out select="name/last" />
          </td>
          <td>
            <x:out select="grade/points" />
          </td>
          <td>
            <x:out select="grade/letter" />
          </td>
        </tr>
      </x:forEach>
    </table>
  </body>
</html>