XML教程
XQuery教程
XSLT教程
XPath教程

XPath 节点功能

XPath节点功能

要与XPath表达式一起使用的节点上的运算符列表:
操作员 说明
/ 用于选择特定节点下的节点。
// 用于从根节点中选择节点。
[...] 用于检查节点值。
| 用于两个节点集的并集。
与XPath表达式一起使用的节点上的函数列表:
功能 说明
node() 用于选择各种节点。
processing-instruction() 用于选择正在处理指令的节点。
text() 用于选择文本节点。
name() 用于提供节点的名称。
position() 用于提供节点的位置。
last() 用于选择相对于当前节点的最后一个节点;
comment() 用于选择作为注释的节点。

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>
    
Employee.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>Serial No</th>
                  <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">
                  <tr>
                     <td><xsl:value-of select = "position()"/></td>
                     <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
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4