XPath谓词

谓词指定用方括号括起来的XPath表达式。在某些情况下, 它用于限制节点集中的选定节点。请参见”雇员”示例中的谓词用法。

employee.xml

<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?>
<class>
   <employee id = "001">
      <firstname>Abhiram</firstname>
      <lastname>Kushwaha</lastname>
      <nickname>Manoj</nickname>
      <salary>15000</salary>
   </employee>
   <employee id = "002">
      <firstname>Akash</firstname>
      <lastname>Singh</lastname>
      <nickname>Bunty</nickname>
      <salary>25000</salary>
   </employee>
    <employee id = "003">
      <firstname>Brijesh</firstname>
      <lastname>Kaushik</lastname>
      <nickname>Ballu</nickname>
      <salary>20000</salary>
   </employee>
    <employee id = "004">
      <firstname>Zoya</firstname>
      <lastname>Mansoori</lastname>
      <nickname>Sonam</nickname>
      <salary>30000</salary>
   </employee>
</class>
Index Predicate Description
1) /class/employee[1] 它将选择第一个employee元素, 它是class元素的子元素。
2) /class/employee[last()] 它将选择为class元素的子元素的最后一个employee元素。
3) / class / employee [@id = 002] 它将选择id为002的员工元素。
4) / class / salary [salary> 10000] 它将选择薪金大于10000的员工元素。

XPath谓词示例

让我们以一个示例为例, 通过遍历每个员工来创建一个<employee>元素及其详细信息的表。本示例使用谓词计算雇员节点的位置, 然后打印雇员详细信息。

employee.xml

<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?>
<class>
   <employee id = "001">
      <firstname>Abhiram</firstname>
      <lastname>Kushwaha</lastname>
      <nickname>Manoj</nickname>
      <salary>15000</salary>
   </employee>
   <employee id = "002">
      <firstname>Akash</firstname>
      <lastname>Singh</lastname>
      <nickname>Bunty</nickname>
      <salary>25000</salary>
   </employee>
    <employee id = "003">
      <firstname>Brijesh</firstname>
      <lastname>Kaushik</lastname>
      <nickname>Ballu</nickname>
      <salary>20000</salary>
   </employee>
    <employee id = "004">
      <firstname>Zoya</firstname>
      <lastname>Mansoori</lastname>
      <nickname>Sonam</nickname>
      <salary>30000</salary>
   </employee>
</class>

员工.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>Employee</h2>
            <table border = "1">
               <tr bgcolor = "pink">	 
                  <th>ID</th>
                  <th>First Name</th>
                  <th>Last Name</th>
                  <th>Nick Name</th>
                  <th>Salary</th>	  
               </tr>					
               <xsl:for-each select = "/class/employee[1]">
                  <tr>	 
                     <td><xsl:value-of select = "@id"/></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 = "salary"/></td>	 
                  </tr>	
               </xsl:for-each>					
               <xsl:for-each select = "/class/employee[last()]">
                  <tr>	 
                     <td><xsl:value-of select = "@id"/></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 = "salary"/></td>	 
                  </tr>	
               </xsl:for-each>	
               <xsl:for-each select = "/class/employee[@id = 002]">
                  <tr>	 
                     <td><xsl:value-of select = "@id"/></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 = "salary"/></td>	 
                  </tr>	
               </xsl:for-each>
               <xsl:for-each select = "/class/employee[salary > 10000]">
                  <tr>	 
                     <td><xsl:value-of select = "@id"/></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 = "salary"/></td>	 
                  </tr>	
               </xsl:for-each>		
            </table> 
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

输出

XPATH谓词1

注意:在上面的示例中, 你可以看到所有四个查询均已提取。

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?