当位置路径使用绝对路径或相对路径定义节点的位置时,轴用于通过元素之间的关系来标识元素,例如父,子,同级节点等。
以下是各种Axis值的列表。
S.No. | Axis & 描述 |
---|---|
1 |
ancestor 表示当前节点的祖先,包括直到根节点的父代。 |
2 |
ancestor-or-self 表示当前节点及其祖先。 |
3 |
attribute 表示当前节点的属性。 |
4 |
child 表示当前节点的子节点。 |
5 |
descendant 表示当前节点的子节点直到叶节点(没有更多的子节点)。 |
6 |
descendant-or-self 表示当前节点及其子节点。 |
7 |
following 表示在当前节点之后的所有节点。 |
8 |
following-sibling 表示上下文节点的以下同级。 |
9 |
namespace 表示当前节点的名称空间。 |
10 |
parent 表示当前节点的父级。 |
11 |
preceding 表示当前节点之前的所有节点。 |
12 |
self 表示当前节点。 |
以下是Axes的一些示例。
firstname - 选择与学生节点相关的名字。
<p><xsl:value-of select = "firstname"/></p> <xsl:value-of select = "/class/student/preceding-sibling::comment()"/>
在此示例中,我们创建了一个样本XML文档 students.xml 及其样式表文档 students.xsl ,该文档使用XPath表达式。
<?xml version = "1.0"?> <?xml-stylesheet type = "text/xsl" href = "students.xsl"?> <class> <!-- Comment: This is a list of student --> <student rollno = "393"> <firstname>Dinkar</firstname> <lastname>Kad</lastname> <nickname>Dinkar</nickname> <marks>85</marks> </student> <student rollno = "493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>Vinni</nickname> <marks>95</marks> </student> <student rollno = "593"> <firstname>Jasvir</firstname> <lastname>Singh</lastname> <nickname>Jazz</nickname> <marks>90</marks> </student> </class>
<?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> <xsl:value-of select = "/class/student/preceding-sibling::comment()"/> <br/> <xsl:text>First Student: </xsl:text> <xsl:value-of select = "/class/student/child::firstname" /> </body> </html> </xsl:template> </xsl:stylesheet>
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)