个性化阅读
专注于IT技术分析

XPath绝对路径

有两种类型的位置路径用于指定XML文档中节点的位置。这些路径是绝对路径或相对路径。

绝对路径以根节点或” /”开头。

/ company / employee-将在类根节点内选择雇员节点。

<xsl:for-each select = "/class/employee">

/公司/员工/名字?它将在类根节点内选择一个雇员节点的名字。

<p><xsl:value-of select = "/class/employee/firstname"/></p>

XPath绝对路径示例

让我们举一个使用绝对路径定位元素的示例。创建一个使用XPath表达式的XML文档employee.xml及其样式表文档employee.xsl。

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>
            <h3>Details of each Employee. </h3>
            <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>						
               <tr>
                  <td><xsl:value-of select = "/class/employee[1]/@id"/></td>
                  <td><xsl:value-of select = "/class/employee[1]/firstname"/></td>
                  <td><xsl:value-of select = "/class/employee[1]/lastname"/></td>
                  <td><xsl:value-of select = "/class/employee[1]/nickname"/></td>
                  <td><xsl:value-of select = "/class/employee[1]/salary"/></td>		 
               </tr>					
               <tr>
                  <td><xsl:value-of select = "/class/employee[2]/@id"/></td>
                  <td><xsl:value-of select = "/class/employee[2]/firstname"/></td>
                  <td><xsl:value-of select = "/class/employee[2]/lastname"/></td>
                  <td><xsl:value-of select = "/class/employee[2]/nickname"/></td>
                  <td><xsl:value-of select = "/class/employee[2]/salary"/></td>		 
               </tr>		
                <tr>
                  <td><xsl:value-of select = "/class/employee[3]/@id"/></td>
                  <td><xsl:value-of select = "/class/employee[3]/firstname"/></td>
                  <td><xsl:value-of select = "/class/employee[3]/lastname"/></td>
                  <td><xsl:value-of select = "/class/employee[3]/nickname"/></td>
                  <td><xsl:value-of select = "/class/employee[3]/salary"/></td>		 
               </tr>		 
               <tr>
                  <td><xsl:value-of select = "/class/employee[4]/@id"/></td>
                  <td><xsl:value-of select = "/class/employee[4]/firstname"/></td>
                  <td><xsl:value-of select = "/class/employee[4]/lastname"/></td>
                  <td><xsl:value-of select = "/class/employee[4]/nickname"/></td>
                  <td><xsl:value-of select = "/class/employee[4]/salary"/></td>		 
               </tr>		
            </table>    
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

输出

Xpath绝对路径1
赞(0) 打赏
未经允许不得转载:srcmini » XPath绝对路径
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

微信扫一扫打赏