XPath Predicates

XPath Predicates identifier expression for finding XPath node in XML document. XPath predicates identifier location path return boolean result either True or False.

you can use any relation operators <, >, <=, >=, =, != and boolean operator or, and.

XPath Predicates Identifier For Finding XPath Node

XPath Predicates Identifier For Finding XPath Node

Following are some predicates identifier expression for finding XPath nodes.

Expression Description
element_name[N] Opening and closing square bracket referring to a specific element sequence number (array sequence).
empinfo/employee[2] Second Child of child element
Select second employee of empinfo element.
empinfo/employee[2]/name Sequence of child of child element
Select name element of second employee element of empinfo element.
element_name[@attribute_name]
empinfo/employee[@id]
Select element with attribute
Select all employee element with given id attribute.
element_name[@attribute_name=value]
empinfo/employee[@id=2]
Select element with specified attribute value
Finding employee elements with given attribute and value. Matching only those elements whose attribute and value are specified in XPath expression.
empinfo/employee[@id=2][2] Select all employee element of given attribute and value. At last select only second employee element.
empinfo/employee[1][@id=1] Select first employee element with given attribute and value.
//designation[@discipline and @experience] Selects all the designation element with given first and second both attribute.
//designation[@discipline or @experience]
//designation[@discipline | @experience]
Select all the designation element with given either first or second, both attribute.

empinfo/employee[2]: Select second employee of empinfo element.

Expression relation: Second Child of child element

XPath selection: 8 to 12 lines

empinfo/employee[2]

<?xml version="1.0" standalone="yes"?>
<empinfo>
  <employee id="1">
    <name>Opal Kole</name>
    <designation discipline="web" experience="3 year">Senior Engineer</designation>
    <email>[email protected]</email>
  </employee>
  <employee id="2">
    <name from="CA">Max Miller</name>
    <designation discipline="DBA" experience="2 year">DBA Engineer</designation>
    <email>[email protected]</email>
  </employee>
  <employee id="3">
    <name>Beccaa Moss</name>
    <designation discipline="appdev">Application Developer</designation>
    <email>[email protected]</email>
  </employee>
</empinfo>

empinfo/employee[2]/name: Select name element of second employee element of empinfo element.

Expression relation: Sequence of child of child element

XPath selection: 9 line

empinfo/employee[2]/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>[email protected]</email>
  </employee>
  <employee id="2">
    <name from="CA">Max Miller</name>
    <designation discipline="DBA" experience="2 year">DBA Engineer</designation>
    <email>[email protected]</email>
  </employee>
  <employee id="3">
    <name>Beccaa Moss</name>
    <designation discipline="appdev">Application Developer</designation>
    <email>[email protected]</email>
  </employee>
</empinfo>

empinfo/employee[@id]: Select all employee element with given id attribute.

Expression relation: Select element with attribute

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

empinfo/employee[@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>[email protected]</email>
  </employee>
  <employee id="2">
    <name from="CA">Max Miller</name>
    <designation discipline="DBA" experience="2 year">DBA Engineer</designation>
    <email>[email protected]</email>
  </employee>
  <employee id="3">
    <name>Beccaa Moss</name>
    <designation discipline="appdev">Application Developer</designation>
    <email>[email protected]</email>
  </employee>
</empinfo>

empinfo/employee[@id=2]: Finding employee elements with given attribute and value. Matching only those elements whose attribute and value are specified in XPath expression.

Expression relation: Select element with specified attribute value

XPath selection: 8 to 12 lines

empinfo/employee[@id=2]

<?xml version="1.0" standalone="yes"?>
<empinfo>
  <employee id="1">
    <name>Opal Kole</name>
    <designation discipline="web" experience="3 year">Senior Engineer</designation>
    <email>[email protected]</email>
  </employee>
  <employee id="2">
    <name from="CA">Max Miller</name>
    <designation discipline="DBA" experience="2 year">DBA Engineer</designation>
    <email>[email protected]</email>
  </employee>
  <employee id="3">
    <name>Beccaa Moss</name>
    <designation discipline="appdev">Application Developer</designation>
    <email>[email protected]</email>
  </employee>
</empinfo>

empinfo/employee[@id=2][2]: Select all employee element of given attribute and value. At last select only second employee element.

XPath selection: 13 to 17 lines

empinfo/employee[@id=2][2]

<?xml version="1.0" standalone="yes"?>
<empinfo>
  <employee id="1">
    <name>Opal Kole</name>
    <designation discipline="web" experience="3 year">Senior Engineer</designation>
    <email>[email protected]</email>
  </employee>
  <employee id="2">
    <name from="CA">Max Miller</name>
    <designation discipline="DBA" experience="2 year">DBA Engineer</designation>
    <email>[email protected]</email>
  </employee>
  <employee id="2">
    <name>Beccaa Moss</name>
    <designation discipline="appdev">Application Developer</designation>
    <email>[email protected]</email>
  </employee>
</empinfo>

empinfo/employee[1][@id=1]: Select first employee element with given attribute and value.

XPath selection: 3 to 7 lines

empinfo/employee[1][@id=1]

<?xml version="1.0" standalone="yes"?>
<empinfo>
  <employee id="1">
    <name>Opal Kole</name>
    <designation discipline="web" experience="3 year">Senior Engineer</designation>
    <email>[email protected]</email>
  </employee>
  <employee id="2">
    <name from="CA">Max Miller</name>
    <designation discipline="DBA" experience="2 year">DBA Engineer</designation>
    <email>[email protected]</email>
  </employee>
  <employee id="3">
    <name>Beccaa Moss</name>
    <designation discipline="appdev">Application Developer</designation>
    <email>[email protected]</email>
  </employee>
</empinfo>

//designation[@discipline and @experience]: Selects all the designation element with given first and second both attribute.

XPath selection: 5, 10 lines

//designation[@discipline and @experience]

<?xml version="1.0" standalone="yes"?>
<empinfo>
  <employee id="1">
    <name>Opal Kole</name>
    <designation discipline="web" experience="3 year">Senior Engineer</designation>
    <email>[email protected]</email>
  </employee>
  <employee id="2">
    <name from="CA">Max Miller</name>
    <designation discipline="DBA" experience="2 year">DBA Engineer</designation>
    <email>[email protected]</email>
  </employee>
  <employee id="3">
    <name>Beccaa Moss</name>
    <designation discipline="appdev">Application Developer</designation>
    <email>[email protected]</email>
  </employee>
</empinfo>

//designation[@discipline or @experience]: Select all the designation element with given either first or second, both attribute.

XPath selection: 5, 10, 15 lines

//designation[@discipline or @experience]

<?xml version="1.0" standalone="yes"?>
<empinfo>
  <employee id="1">
    <name>Opal Kole</name>
    <designation discipline="web" experience="3 year">Senior Engineer</designation>
    <email>[email protected]</email>
  </employee>
  <employee id="2">
    <name from="CA">Max Miller</name>
    <designation discipline="DBA" experience="2 year">DBA Engineer</designation>
    <email>[email protected]</email>
  </employee>
  <employee id="3">
    <name>Beccaa Moss</name>
    <designation discipline="appdev">Application Developer</designation>
    <email>[email protected]</email>
  </employee>
</empinfo>

//designation[@discipline | @experience]: Select all the designation element with given either first or second, both attribute.

XPath selection: 5, 10, 15 lines

//designation[@discipline | @experience]

<?xml version="1.0" standalone="yes"?>
<empinfo>
  <employee id="1">
    <name>Opal Kole</name>
    <designation discipline="web" experience="3 year">Senior Engineer</designation>
    <email>[email protected]</email>
  </employee>
  <employee id="2">
    <name from="CA">Max Miller</name>
    <designation discipline="DBA" experience="2 year">DBA Engineer</designation>
    <email>[email protected]</email>
  </employee>
  <employee id="3">
    <name>Beccaa Moss</name>
    <designation discipline="appdev">Application Developer</designation>
    <email>[email protected]</email>
  </employee>
</empinfo>