XPath謂詞


謂詞是指用方括號寫的XPath表示式。 它指的是為某些條件限制節點集中的選定節點。 例如,

序號 序號 描述
1 /class/student[1] 選擇第一個student元素,它是class元素的子元素。
2 /class/student[last()] 選擇最後一個student元素,它是class元素的子元素。
3 /class/student[@rolllno = 493] 使用rollno=493選擇student元素。
4 /class/student[marks>85] 選擇marks大於85student元素。

範例

此範例通過疊代每個學生建立一個包含其詳細資訊的<student>元素。 它計算student節點的位置,然後列印學生的詳細資訊以及序列號。

檔案: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>學生資訊列表如下 -</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[1]">
                  <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>

               <xsl:for-each select = "/class/student[last()]">
                  <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>

               <xsl:for-each select = "/class/student[@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>

               <xsl:for-each select = "/class/student[marks > 85]">
                  <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,效果如下所示 -