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,效果如下所示 -


上一篇:XPath通配符

下一篇:没有了

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程