XPath謂詞

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

序號

序號

描述

1

/class/student[1]

選擇第一個student元素,它是class元素的子元素。

2

/class/student[last()]

選擇最後一個student元素,它是class元素的子元素。

3

/class/student[[@rolllno](https://github.com/rolllno "@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,效果如下所示 -

XPath謂詞

0 條評論,你可以發表評論,我們會進行改進
Comment author placeholder