我們能在 JSP 中測試 XPath 表示式嗎?
<x:if> 標記評估一個XPath 表示式測試,如果為真,它將處理其正文。如果測試條件為假,則忽略正文。
屬性
<x:if> 標記有以下屬性 −
屬性 | 描述 | 必需 | 預設 |
---|---|---|---|
select | 要評估的 XPath 表示式 | 是 | 無 |
var | 用於儲存條件結果的變數的名稱 | 否 | 無 |
scope | var 屬性中指定變數的作用域 | 否 | 頁面 |
示例
以下是顯示<x:if> 標記的示例 −
<%@ 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 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> <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>
現在,讓我們訪問上面的 JSP,將顯示以下結果 −
Books Info: Document has at least one <book> element. Book prices are very high
廣告