XML Tutorial

DTD Tutorial

XPath Tutorial



XPath Node Test Examples


XPath node test is a part of XPath expression for finding nodes in XML document.

Select Axis Description
//name/self::* Select the name context node.
child::* Select All child nodes of the context node.
child::node() Select all child nodes of the context node.
child::empinfo Select all child elements of empinfo node.
//employee/descendant::* Select all descendant of the employee node.
//descendant::employee Select all descendant of the employee node in context node.
//employee/descendant-or-self::* Select all descendant of the employee nodes and context node itself.
//descendant-or-self::employee Select all descendant of employee node with context node itself.

Ancestor

//employee/ancestor::* Select all ancestor node of the employee node.
//ancestor::name Select all ancestor of the name node in context node.
//employee/ancestor-or-self::* Select all ancestor of the employee nodes and context node itself.
//name/ancestor-or-self::employee Select all ancestor of name node with context node itself.
//name/parent::* Select parent node of the name context node.
//name/parent::employee Return result node if employee node is parent node of the context node, otherwise no node found.
//attribute::id Select all node with id attribute.
//attribute::* Select all node with any attribute.
//employee[@id=1]/following::* Select all nodes (with child nodes) after the context node.
//employee[@id=1]/following-sibling::* Select all sibling nodes after the context node.
//employee[@id=3]/preceding::* Select all nodes (with child nodes) before the context node.
//employee[@id=3]/preceding-sibling::* Select all sibling nodes before the context node.

Following are some XPath node test examples.

//name/self::* : Select the name context node.

XPath selection : name node value (4, 9, 14 lines).

//name/self::*

<?xml version="1.0" standalone="yes"?>
<empinfo>
	<employee id="1">
		<name>Opal Kole</name>
		<designation discipline="web" experience="3 year">Senior Engineer</designation>
		<email>OpalKole@myemail.com</email>
	</employee>
	<employee id="2">
		<name from="CA">Max Miller</name>
		<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
		<email>maxmiller@email.com</email>
	</employee>
	<employee id="3">
		<name>Beccaa Moss</name>
		<designation discipline="appdev">Application Developer</designation>
		<email>beccaamoss@email.com</email>
	</employee>
</empinfo>

child::* : Select All child nodes of the context node.

XPath selection : 3 to 7, 8 to 12, 13 to 17 lines.

/empinfo/child::*

<?xml version="1.0" standalone="yes"?>
<empinfo>
	<employee id="1">
		<name>Opal Kole</name>
		<designation discipline="web" experience="3 year">Senior Engineer</designation>
		<email>OpalKole@myemail.com</email>
	</employee>
	<employee id="2">
		<name from="CA">Max Miller</name>
		<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
		<email>maxmiller@email.com</email>
	</employee>
	<employee id="3">
		<name>Beccaa Moss</name>
		<designation discipline="appdev">Application Developer</designation>
		<email>beccaamoss@email.com</email>
	</employee>
</empinfo>

child::node() : Select all child nodes of the context node.

XPath selection : name node value (4, 9, 14 lines).

child::node()

<?xml version="1.0" standalone="yes"?>
<empinfo>
	<employee id="1">
		<name>Opal Kole</name>
		<designation discipline="web" experience="3 year">Senior Engineer</designation>
		<email>OpalKole@myemail.com</email>
	</employee>
	<employee id="2">
		<name from="CA">Max Miller</name>
		<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
		<email>maxmiller@email.com</email>
	</employee>
	<employee id="3">
		<name>Beccaa Moss</name>
		<designation discipline="appdev">Application Developer</designation>
		<email>beccaamoss@email.com</email>
	</employee>
</empinfo>

child::empinfo : Select all child elements of empinfo node.

XPath selection : 2 to 18 lines.

child::empinfo

<?xml version="1.0" standalone="yes"?>
<empinfo>
	<employee id="1">
		<name>Opal Kole</name>
		<designation discipline="web" experience="3 year">Senior Engineer</designation>
		<email>OpalKole@myemail.com</email>
	</employee>
	<employee id="2">
		<name from="CA">Max Miller</name>
		<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
		<email>maxmiller@email.com</email>
	</employee>
	<employee id="3">
		<name>Beccaa Moss</name>
		<designation discipline="appdev">Application Developer</designation>
		<email>beccaamoss@email.com</email>
	</employee>
</empinfo>

//employee/descendant::* : Select all descendant of the employee node.

XPath selection : 4, 5, 6, 9, 10, 11, 14, 15, 16 lines.

//employee/descendant::*

<?xml version="1.0" standalone="yes"?>
<empinfo>
	<employee id="1">
		<name>Opal Kole</name>
		<designation discipline="web" experience="3 year">Senior Engineer</designation>
		<email>OpalKole@myemail.com</email>
	</employee>
	<employee id="2">
		<name from="CA">Max Miller</name>
		<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
		<email>maxmiller@email.com</email>
	</employee>
	<employee id="3">
		<name>Beccaa Moss</name>
		<designation discipline="appdev">Application Developer</designation>
		<email>beccaamoss@email.com</email>
	</employee>
</empinfo>

//descendant::employee : Select all descendant of the employee node in context node.

XPath selection : 3 to 7, 8 to 12, 13 to 17 lines.

//descendant::employee

<?xml version="1.0" standalone="yes"?>
<empinfo>
	<employee id="1">
		<name>Opal Kole</name>
		<designation discipline="web" experience="3 year">Senior Engineer</designation>
		<email>OpalKole@myemail.com</email>
	</employee>
	<employee id="2">
		<name from="CA">Max Miller</name>
		<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
		<email>maxmiller@email.com</email>
	</employee>
	<employee id="3">
		<name>Beccaa Moss</name>
		<designation discipline="appdev">Application Developer</designation>
		<email>beccaamoss@email.com</email>
	</employee>
</empinfo>

//employee/descendant-or-self::* : Select all descendant of the employee nodes and context node itself.

XPath selection : 4, 5, 6, 8, 9, 10, 11, 13, 14, 15, 16 lines.

//employee/descendant-or-self::*

<?xml version="1.0" standalone="yes"?>
<empinfo>
	<employee id="1">
		<name>Opal Kole</name>
		<designation discipline="web" experience="3 year">Senior Engineer</designation>
		<email>OpalKole@myemail.com</email>
	</employee>
	<employee id="2">
		<name from="CA">Max Miller</name>
		<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
		<email>maxmiller@email.com</email>
	</employee>
	<employee id="3">
		<name>Beccaa Moss</name>
		<designation discipline="appdev">Application Developer</designation>
		<email>beccaamoss@email.com</email>
	</employee>
</empinfo>

//descendant-or-self::employee : Select all descendant of employee node with context node itself.

XPath selection : 3 to 7, 8 to 12, 13 to 17 lines.

//descendant-or-self::employee

<?xml version="1.0" standalone="yes"?>
<empinfo>
	<employee id="1">
		<name>Opal Kole</name>
		<designation discipline="web" experience="3 year">Senior Engineer</designation>
		<email>OpalKole@myemail.com</email>
	</employee>
	<employee id="2">
		<name from="CA">Max Miller</name>
		<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
		<email>maxmiller@email.com</email>
	</employee>
	<employee id="3">
		<name>Beccaa Moss</name>
		<designation discipline="appdev">Application Developer</designation>
		<email>beccaamoss@email.com</email>
	</employee>
</empinfo>

//employee/ancestor::* : Select all ancestor node of the employee node.

XPath selection : 2 to 18 lines.

//employee/ancestor::*

<?xml version="1.0" standalone="yes"?>
<empinfo>
	<employee id="1">
		<name>Opal Kole</name>
		<designation discipline="web" experience="3 year">Senior Engineer</designation>
		<email>OpalKole@myemail.com</email>
	</employee>
	<employee id="2">
		<name from="CA">Max Miller</name>
		<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
		<email>maxmiller@email.com</email>
	</employee>
	<employee id="3">
		<name>Beccaa Moss</name>
		<designation discipline="appdev">Application Developer</designation>
		<email>beccaamoss@email.com</email>
	</employee>
</empinfo>

//ancestor::name : Select all ancestor of the name node in context node.

XPath selection : name node value (4, 9, 14 lines).

//ancestor::name

<?xml version="1.0" standalone="yes"?>
<empinfo>
	<employee id="1">
		<name>Opal Kole</name>
		<designation discipline="web" experience="3 year">Senior Engineer</designation>
		<email>OpalKole@myemail.com</email>
	</employee>
	<employee id="2">
		<name from="CA">Max Miller</name>
		<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
		<email>maxmiller@email.com</email>
	</employee>
	<employee id="3">
		<name>Beccaa Moss</name>
		<designation discipline="appdev">Application Developer</designation>
		<email>beccaamoss@email.com</email>
	</employee>
</empinfo>

//employee/ancestor-or-self::* : Select all ancestor of the employee nodes and context node itself.

XPath selection : 2, 3, 7, 8, 12, 13, 17, 18 lines (4 node select).

//employee/ancestor-or-self::*

<?xml version="1.0" standalone="yes"?>
<empinfo>
	<employee id="1">
		<name>Opal Kole</name>
		<designation discipline="web" experience="3 year">Senior Engineer</designation>
		<email>OpalKole@myemail.com</email>
	</employee>
	<employee id="2">
		<name from="CA">Max Miller</name>
		<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
		<email>maxmiller@email.com</email>
	</employee>
	<employee id="3">
		<name>Beccaa Moss</name>
		<designation discipline="appdev">Application Developer</designation>
		<email>beccaamoss@email.com</email>
	</employee>
</empinfo>

//name/ancestor-or-self::employee : Select all ancestor of name node with context node itself.

XPath selection : 3 to 7, 8 to 12, 13 to 17 lines (3 node select).

//name/ancestor-or-self::employee

<?xml version="1.0" standalone="yes"?>
<empinfo>
	<employee id="1">
		<name>Opal Kole</name>
		<designation discipline="web" experience="3 year">Senior Engineer</designation>
		<email>OpalKole@myemail.com</email>
	</employee>
	<employee id="2">
		<name from="CA">Max Miller</name>
		<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
		<email>maxmiller@email.com</email>
	</employee>
	<employee id="3">
		<name>Beccaa Moss</name>
		<designation discipline="appdev">Application Developer</designation>
		<email>beccaamoss@email.com</email>
	</employee>
</empinfo>

//name/parent::* : Select parent node of the name context node.

XPath selection : 3 to 7, 8 to 12, 13 to 17 lines (3 node select).

//name/parent::*

<?xml version="1.0" standalone="yes"?>
<empinfo>
	<employee id="1">
		<name>Opal Kole</name>
		<designation discipline="web" experience="3 year">Senior Engineer</designation>
		<email>OpalKole@myemail.com</email>
	</employee>
	<employee id="2">
		<name from="CA">Max Miller</name>
		<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
		<email>maxmiller@email.com</email>
	</employee>
	<employee id="3">
		<name>Beccaa Moss</name>
		<designation discipline="appdev">Application Developer</designation>
		<email>beccaamoss@email.com</email>
	</employee>
</empinfo>

//name/parent::employee : Return result node if employee node is parent node of the context node, otherwise no node found.

XPath selection : 3 to 7, 8 to 12, 13 to 17 lines (3 node select).

//name/parent::employee

<?xml version="1.0" standalone="yes"?>
<empinfo>
	<employee id="1">
		<name>Opal Kole</name>
		<designation discipline="web" experience="3 year">Senior Engineer</designation>
		<email>OpalKole@myemail.com</email>
	</employee>
	<employee id="2">
		<name from="CA">Max Miller</name>
		<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
		<email>maxmiller@email.com</email>
	</employee>
	<employee id="3">
		<name>Beccaa Moss</name>
		<designation discipline="appdev">Application Developer</designation>
		<email>beccaamoss@email.com</email>
	</employee>
</empinfo>

//attribute::id : Select all node with id attribute.

XPath selection : id attribute value (3, 8, 13 lines).

//attribute::id

<?xml version="1.0" standalone="yes"?>
<empinfo>
	<employee id="1">
		<name>Opal Kole</name>
		<designation discipline="web" experience="3 year">Senior Engineer</designation>
		<email>OpalKole@myemail.com</email>
	</employee>
	<employee id="2">
		<name from="CA">Max Miller</name>
		<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
		<email>maxmiller@email.com</email>
	</employee>
	<employee id="3">
		<name>Beccaa Moss</name>
		<designation discipline="appdev">Application Developer</designation>
		<email>beccaamoss@email.com</email>
	</employee>
</empinfo>

//attribute::* : Select all node with any attribute.

XPath selection : select all attribute value in highlighted lines.

//attribute::*

<?xml version="1.0" standalone="yes"?>
<empinfo>
	<employee id="1">
		<name>Opal Kole</name>
		<designation discipline="web" experience="3 year">Senior Engineer</designation>
		<email>OpalKole@myemail.com</email>
	</employee>
	<employee id="2">
		<name from="CA">Max Miller</name>
		<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
		<email>maxmiller@email.com</email>
	</employee>
	<employee id="3">
		<name>Beccaa Moss</name>
		<designation discipline="appdev">Application Developer</designation>
		<email>beccaamoss@email.com</email>
	</employee>
</empinfo>

//employee[@id=1]/following::* : Select all nodes (with child nodes) after the context node.

XPath selection : 8 to 12, 9, 10, 11, 13 to 17, 14, 15, 16 lines (8 node select).

//employee[@id=1]/following::*

<?xml version="1.0" standalone="yes"?>
<empinfo>
	<employee id="1">
		<name>Opal Kole</name>
		<designation discipline="web" experience="3 year">Senior Engineer</designation>
		<email>OpalKole@myemail.com</email>
	</employee>
	<employee id="2">
		<name from="CA">Max Miller</name>
		<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
		<email>maxmiller@email.com</email>
	</employee>
	<employee id="3">
		<name>Beccaa Moss</name>
		<designation discipline="appdev">Application Developer</designation>
		<email>beccaamoss@email.com</email>
	</employee>
</empinfo>

//employee[@id=1]/following-sibling::* : Select all sibling nodes after the context node.

XPath selection : 8 to 12, 13 to 17 lines (2 node select).

//employee[@id=1]/following-sibling::*

<?xml version="1.0" standalone="yes"?>
<empinfo>
	<employee id="1">
		<name>Opal Kole</name>
		<designation discipline="web" experience="3 year">Senior Engineer</designation>
		<email>OpalKole@myemail.com</email>
	</employee>
	<employee id="2">
		<name from="CA">Max Miller</name>
		<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
		<email>maxmiller@email.com</email>
	</employee>
	<employee id="3">
		<name>Beccaa Moss</name>
		<designation discipline="appdev">Application Developer</designation>
		<email>beccaamoss@email.com</email>
	</employee>
</empinfo>

//employee[@id=3]/preceding::* : Select all nodes (with child nodes) before the context node.

XPath selection : 3 to 7, 4, 5, 6, 8 to 12, 9, 10, 11 lines (8 node select).

//employee[@id=3]/preceding::*

<?xml version="1.0" standalone="yes"?>
<empinfo>
	<employee id="1">
		<name>Opal Kole</name>
		<designation discipline="web" experience="3 year">Senior Engineer</designation>
		<email>OpalKole@myemail.com</email>
	</employee>
	<employee id="2">
		<name from="CA">Max Miller</name>
		<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
		<email>maxmiller@email.com</email>
	</employee>
	<employee id="3">
		<name>Beccaa Moss</name>
		<designation discipline="appdev">Application Developer</designation>
		<email>beccaamoss@email.com</email>
	</employee>
</empinfo>

//employee[@id=3]/preceding-sibling::* : Select all sibling nodes before the context node.

XPath selection : 3 to 7, 8 to 12 lines (2 node select).

//employee[@id=3]/preceding-sibling::*

<?xml version="1.0" standalone="yes"?>
<empinfo>
	<employee id="1">
		<name>Opal Kole</name>
		<designation discipline="web" experience="3 year">Senior Engineer</designation>
		<email>OpalKole@myemail.com</email>
	</employee>
	<employee id="2">
		<name from="CA">Max Miller</name>
		<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
		<email>maxmiller@email.com</email>
	</employee>
	<employee id="3">
		<name>Beccaa Moss</name>
		<designation discipline="appdev">Application Developer</designation>
		<email>beccaamoss@email.com</email>
	</employee>
</empinfo>