如果路径以我们选择的节点开始, 则称为相对路径。
请参阅此语法, 该语法指定使用到雇员节点的相对路径来定位元素。
<p><xsl:value-of select = "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>
<xsl:for-each select = "/class/employee">
<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>
输出
评论前必须登录!
注册