As we know that path defines the location of a node using absolute or relative path. In the same manner XPath axes are used to identify elements by their relationship like parent, child, sibling, etc. Axes refer to axis on which elements are lying relative to an element.
A list of various Axis values:
Index | Axis | Description |
---|---|---|
1) | ancestor | It specifies the ancestors of the current nodes which include the parents up to the root node. |
2) | ancestor-or-self | It specifies the current node and its ancestors. |
3) | attribute | It specifies the attributes of the current node. |
4) | child | It specifies the children of the current node. |
5) | descendant | It specifies the descendants of the current node i.e. the node's children up to the leaf node(no more children). |
6) | descendant-or-self | It specifies the current node and it's descendants. |
7) | following | It specifies all nodes that come after the current node. |
8) | following-sibling | It specifies the following siblings of the context node. Siblings are at the same level as the current node and share it's parent. |
9) | namespace | It specifies the namespace of the current node. |
10) | parent | It specifies the parent of the current node. |
11) | preceding | It specifies all nodes that come before the current node (i.e. before it's opening tag). |
12) | self | It specifies the current node. |
<p><xsl:value-of select = "firstname"/></p> <xsl:value-of select = "/class/student/preceding-sibling::comment()"/>
Here the firstname is related to employee node of the XML document named employee.xml
Let's create an XML document named "employee.xml" and its stylesheet document named "employee.xsl" which uses the XPath expression.
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>
Employee.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/employee/preceding-sibling::comment()"/> <br/> <xsl:text>First Employee: </xsl:text> <xsl:value-of select = "/class/employee/child::firstname" /> </body> </html> </xsl:template> </xsl:stylesheet>
Output: