XPath布林運算子

2019-10-16 23:18:44

XPath定義了以下與XPath表示式一起使用的布林運算子。

序號 運算子 描述
1 and 兩個條件都要滿足
2 or 任何一個要滿足的條件
3 not() 檢查條件未得到滿足函式。

範例

此範例通過疊代每個學生建立一個<student>元素的表,<student>屬性:rollno,其子<firstname><lastname><nickname><marks>。 它將檢查rollno值為393493,然後列印學生的詳細資訊。

檔案:students.xml -

<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "students.xsl"?>
<class>
   <student rollno = "393">
      <firstname>Dinkar</firstname>
      <lastname>Su</lastname>
      <nickname>MaXX</nickname>
      <marks>88</marks>
   </student>
   <student rollno = "493">
      <firstname>Vaneet</firstname>
      <lastname>Lee</lastname>
      <nickname>Vicky</nickname>
      <marks>95</marks>
   </student>
   <student rollno = "593">
      <firstname>Jasvir</firstname>
      <lastname>Wong</lastname>
      <nickname>Jazz</nickname>
      <marks>90</marks>
   </student>
</class>

檔案:students.xsl -

<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
   xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">  

   <xsl:template match = "/">
      <html>
         <body>
            <h2>Students</h2>

            <table border = "1">
               <tr bgcolor = "#9acd32">
                  <th>Roll No</th>
                  <th>First Name</th>
                  <th>Last Name</th>
                  <th>Nick Name</th>
                  <th>Marks</th>
               </tr>

               <xsl:for-each select = "class/student[(@rollno = 393) or ((@rollno  =  493))]">

                  <tr>
                     <td><xsl:value-of select = "@rollno"/></td>
                     <td><xsl:value-of select = "firstname"/></td>
                     <td><xsl:value-of select = "lastname"/></td>
                     <td><xsl:value-of select = "nickname"/></td>
                     <td><xsl:value-of select = "marks"/></td>
                  </tr>
               </xsl:for-each>
            </table>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

在瀏覽器中開啟 student.xml 檔案,效果如下所示 -