XPath – Axes

  • Post author:
  • Post category:XPath
  • Post comments:0 Comments

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
1ancestorRepresents the ancestors of the current node which include the parents up to the root node.
2ancestor-or-selfRepresents the current node and it’s ancestors.
3attributeRepresents the attributes of the current node.
4childRepresents the children of the current node.
5descendantRepresents the descendants of the current node. Descendants include the node’s children upto the leaf node(no more children).
6descendant-or-selfRepresents the current node and it’s descendants.
7followingRepresents all nodes that come after the current node.
8following-siblingRepresents the following siblings of the context node. Siblings are at the same level as the current node and share it’s parent.
9namespaceRepresents the namespace of the current node.
10parentRepresents the parent of the current node.
11precedingRepresents all nodes that come before the current node (i.e. before it’s opening tag).
12selfRepresents the current node.

Following are a few examples on the uses of axes.

firstname − select firstname related to student nodes.

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

Example

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

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

Leave a Reply