由于位置路径使用绝对路径或相对路径定义节点的位置,因此轴用于通过它们的关系来识别元素,如父,子,兄弟节点等。轴的命名是因为它们指的是元素相对于元素所在的轴。
以下是各种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 |
表示当前节点。 |
以下是关于轴的使用的几个例子。firstname
- 选择与<student>
节点相关的名字。
<p><xsl:value-of select = firstname/></p>
<xsl:value-of select = /class/student/preceding-sibling::comment()/>
示例
在这个例子中,我们创建了一个示例XML文档students.xml,及其样式表文档students.xsl,它使用了XPath表达式。
以下是使用的示例XML。文件:students.xml -
<?xml version = 1.0?>
<?xml-stylesheet type = text/xsl href = students.xsl?>
<class>
<student rollno = 393>
<firstname>Dinkar</firstname>
<lastname>Su</lastname>
<nickname>MaXX</nickname>
<marks>88</marks>
</student>
<student rollno = 493>
<firstname>Vaneet</firstname>
<lastname>Lee</lastname>
<nickname>Vicky</nickname>
<marks>95</marks>
</student>
<student rollno = 593>
<firstname>Jasvir</firstname>
<lastname>Wong</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
文件:students.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>
<xsl:value-of select = /class/student/preceding-sibling::comment()/>
<br/>
<xsl:text>第一个学生: </xsl:text>
<xsl:value-of select = /class/student/child::firstname />
</body>
</html>
</xsl:template>
</xsl:stylesheet>