我們能在 JSP 中測試 XPath 表示式嗎?


<x:if> 標記評估一個XPath 表示式測試,如果為真,它將處理其正文。如果測試條件為假,則忽略正文。

屬性

<x:if> 標記有以下屬性 −

屬性描述必需預設
select要評估的 XPath 表示式
var用於儲存條件結果的變數的名稱
scopevar 屬性中指定變數的作用域頁面

示例

以下是顯示<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

更新於:2019-07-30

201 次瀏覽

開啟你的 職業

完成課程即可獲得認證

開始
廣告