目錄
x:choose, x:when, x:otherwise標籤
x:out標籤
<x:out>標籤顯示XPath表達式的結果,與<%= %>功能相似。
語法格式
<x:out select="<string>" escapeXml="<true|false>"/>
屬性
<x:out>標籤有如下屬性:
屬性 | 描述 | 是否必要 | 默認值 |
---|---|---|---|
select | 需要計算的XPath表達式,通常使用XPath 變量 | 是 | 無 |
escapeXml | 是否忽略XML 特殊字符 | 否 | true |
實例演示
接下來的例子使用了<x:out>和<x:parse>標籤:
<%@ 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:out 標籤</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>
<x:parse xml="${xmltext}" var="output"/>
<b>The title of the first book is</b>:
<x:out select="$output/books/book[1]/name" />
<br>
<b>The price of the second book</b>:
<x:out select="$output/books/book[2]/price" />
</body>
</html>
運行結果如下:
BOOKS INFO:
The title of the first book is: Padam History
The price of the second book: 2000
x:parse標籤
<x:parse>標籤用來解析屬性中或標籤主體中的XML數據。
語法格式
<x:parse
var="<string>"
varDom="<string>"
scope="<string>"
scopeDom="<string>"
doc="<string>"
systemId="<string>"
filter="<string>"/>
屬性
<x:parse>標籤有如下屬性:
屬性 | 描述 | 是否必要 | 默認值 |
---|---|---|---|
var | 包含已解析XML數據的變量 | 否 | 無 |
xml | 需要解析的文檔的文本內容(String或Reader) | 否 | Body |
systemId | 系統標識符URI,用來解析文檔 | 否 | 無 |
filter | 應用於源文檔的過濾器 | 否 | 無 |
doc | 需要解析的XML文檔 | 否 | Page |
scope | var屬性的作用域 | 否 | Page |
varDom | 包含已解析XML數據的變量 | 否 | Page |
scopeDom | varDom屬性的作用域 | 否 | Page |
實例演示
接下了的例子告訴我們如何解析一個XML文檔:
books.xml文件代碼如下:
<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>
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:parse 標籤</title>
</head>
<body>
<h3>Books Info:</h3>
<c:import var="bookInfo" url="http://localhost:8080/books.xml"/>
<x:parse xml="${bookInfo}" var="output"/>
<b>The title of the first book is</b>:
<x:out select="$output/books/book[1]/name" />
<br>
<b>The price of the second book</b>:
<x:out select="$output/books/book[2]/price" />
</body>
</html>
運行結果如下:
BOOKS INFO:
The title of the first book is:Padam History
The price of the second book: 2000
x:set標籤
<x:set>標籤爲XPath表達式的值設置一個變量。
如果XPath表達式的值是boolean類型,則<x:set>將會設置一個java.lang.Boolean對象,若是字符串,則設置一個java.lang.String對象,若是數字,則設置一個java.lang.Number對象。
語法格式
<x:set var="<string>" select="<string>" scope="<string>"/>
屬性
<x:set>標籤有如下屬性:
屬性 | 描述 | 是否必要 | 默認值 |
---|---|---|---|
var | 代表XPath表達式值得變量 | 是 | Body |
select | 需要計算的XPath表達式 | 否 | 無 |
scope | var屬性的作用域 | 否 | Page |
實例演示
接下來的例子告訴我們如何使用<x:set>標籤:
<%@ 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:set 標籤</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>
<x:parse xml="${xmltext}" var="output"/>
<x:set var="fragment" select="$output//book"/>
<b>The price of the second book</b>:
<c:out value="${fragment}" />
</body>
</html>
運行結果如下:
BOOKS INFO:
The price of the second book:[[book: null], [book: null]]
x:if標籤
<x:if>標籤用於判斷一個XPath表達式的值,若爲真,則執行其主體中的內容,若爲假則其主體的內容將會被忽略。
語法格式
<x:if
select="<string>"
var="<string>"
scope="<string>">
...
</x:if>
屬性
<x:if>標籤有如下屬性:
屬性 | 描述 | 是否必要 | 默認值 |
---|---|---|---|
select | 需要計算的XPath表達式 | 是 | 無 |
var | 存儲條件結果的變量 | 否 | 無 |
scope | var屬性的作用域 | 否 | Page |
實例演示
使用<x:if>標籤:
<%@ 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:if 標籤</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>
<x:parse xml="${xmltext}" var="output"/>
<x:if select="$output//book">
Document has at least one <book> element.
</x:if>
<br />
<x:if select="$output/books[1]/book/price > 100">
Book prices are very high
</x:if>
</body>
</html>
運行結果如下:
BOOKS INFO:
Document has at least one <book> element.
Book prices are very high
x:forEach標籤
<x:forEach>標籤用來循環遍歷XML文檔的節點。
語法格式
<x:forEach
var="<string>"
select="<string>"
begin="<int>"
end="<int>"
step="<int>"
varStatus="<string>">
屬性
<x:forEach>標籤有如下屬性:
屬性 | 描述 | 是否必要 | 默認值 |
---|---|---|---|
select | 需要計算的XPath表達式 | 是 | 無 |
var | 用於存儲當前項目的變量 | 否 | 無 |
begin | 迭代器的開始索引 | 否 | 無 |
end | 迭代器的結束索引 | 否 | 無 |
step | 迭代的步長 | 否 | 無 |
varStatus | 代表迭代器所存儲的狀態的變量 | 否 | 無 |
實例演示
<%@ 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:forEach 標籤</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>
<x:parse xml="${xmltext}" var="output"/>
<ul class="list">
<x:forEach select="$output/books/book/name" var="item">
<li>Book Name: <x:out select="$item" /></li>
</x:forEach>
</ul>
</body>
</html>
運行結果如下:
BOOKS INFO:
Book Name: Padam History
Book Name: Great Mistry
x:choose, x:when, x:otherwise標籤
<x:choose>標籤與Java switch語句有相同的功能。switch語句有case語句,而<x:choose>標籤有<x:when>標籤。switch語句有default語句,而<x:choose>標籤有<x:otherwise>標籤。
語法格式
<x:choose>
<x:when select="<string>">
...
</x:when>
<x:when select="<string>">
...
</x:when>
...
...
<x:otherwise>
...
</x:otherwise>
</x:choose>
屬性
- <x:choose>沒有屬性。
- <x:when>的屬性在下表中給出。
- <x:otherwise>沒有屬性。
<x:when>標籤的屬性:
屬性 | 描述 | 是否必要 | 默認值 |
---|---|---|---|
select | 條件 | 是 | 無 |
實例演示
<%@ 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:choose 標籤</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>
<x:parse xml="${xmltext}" var="output"/>
<x:choose>
<x:when select="$output//book/author = 'ZARA'">
Book is written by ZARA
</x:when>
<x:when select="$output//book/author = 'NUHA'">
Book is written by NUHA
</x:when>
<x:otherwise>
Unknown author.
</x:otherwise>
</x:choose>
</body>
</html>
運行結果如下:
BOOKS INFO:
Book is written by ZARA
x:transform標籤
<x:transform>標籤在XML文檔中應用XSL。
語法格式
<x:transform
var="<string>"
scope="<string>"
result="<string>"
doc="<string>"
docSystemId="<string>"
xslt="<string>"
xsltSystemId="<string>"/>
屬性
<x:transform>標籤有如下屬性:
屬性 | 描述 | 是否必要 | 默認值 |
---|---|---|---|
doc | 源XML文檔 | 否 | Body |
docSystemId | 源XML文檔的URI | 否 | 無 |
xslt | XSLT 樣式表 | 是 | 無 |
xsltSystemId | 源XSLT文檔的URI | 否 | 無 |
result | 接收轉換結果的對象 | 否 | Print to page |
var | 代表被轉換的XML文檔的變量 | 否 | Print to page |
scope | 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>
運行結果如下:
x:param標籤
<x:param>標籤與<x:transform>標籤一同使用,用於設置XSLT樣式表的參數。
語法格式
<x:param name="<string>" value="<string>"/>
屬性
<x:param>標籤有如下屬性:
屬性 | 描述 | 是否必要 | 默認值 |
---|---|---|---|
name | XSLT參數的名稱 | 是 | Body |
value | XSLT參數的值 | 否 | 無 |
實例演示
style.xsl文件代碼如下,使用xsl:param...標籤與{$bgColor}變量:
<?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:param name="bgColor"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="books">
<table border="1" width="50%" bgColor="{$bgColor}">
<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>
mian.jsp文件代碼如下,在x:transform標籤中使用x:param 標籤:
<%@ 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:param 標籤</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}">
<x:param name="bgColor" value="grey"/>
</x:transform>
</body>
</html>
運行結果如下: