Xpath 简明教程

XPath - Axes

由于位置路径使用绝对或相对路径定义节点的位置,因此轴用于按其关系(如 parent, child, sibling, 等)识别元素。之所以这样命名轴,是因为它们引用元素相对于某个元素所在的轴。

As location path defines the location of a node using absolute or relative path, axes are used to identify elements by their relationship like parent, child, sibling, etc. Axes are named so because they refer to axis on which elements are lying relative to an element.

以下是各种轴值的列表。

Following is the list of various Axis values.

S.No.

Axis & Description

1

ancestor Represents the ancestors of the current node which include the parents up to the root node.

2

ancestor-or-self Represents the current node and it’s ancestors.

3

attribute Represents the attributes of the current node.

4

child Represents the children of the current node.

5

descendant Represents the descendants of the current node. Descendants include the node’s children upto the leaf node(no more children).

6

descendant-or-self Represents the current node and it’s descendants.

7

following Represents all nodes that come after the current node.

8

following-sibling Represents the following siblings of the context node. Siblings are at the same level as the current node and share it’s parent.

9

namespace Represents the namespace of the current node.

10

parent Represents the parent of the current node.

11

preceding Represents all nodes that come before the current node (i.e. before it’s opening tag).

12

self Represents the current node.

以下是轴使用的一些示例。

Following are a few examples on the uses of axes.

firstname − 选择与学生节点相关的 firstName。

firstname − select firstname related to student nodes.

<p><xsl:value-of select = "firstname"/></p>
<xsl:value-of select = "/class/student/preceding-sibling::comment()"/>

Example

在此示例中,我们创建了一个 XML 文档示例 students.xml 和使用 XPath 表达式的样式表文档 students.xsl

In this example, we’ve created a sample XML document students.xml and its stylesheet document students.xsl which uses the XPath expressions.

下面是使用的 XML 示例。

Following is the sample XML used.

students.xml

<?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>

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>First Student: </xsl:text>
            <xsl:value-of select = "/class/student/child::firstname" />
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

Verify the output

xpath axes