XPathParser Function

Number of input and output field

Number of input field : One
Number of output field : One

XPathParser is used to extract values from XML data using XPath expressions. An XPath expression can return a nodeset, string, Boolean, or number.



Parameters used by XPathParser:
To insert a field’s content into an XPath expression, use the syntax {field_index}. For example,[name='{1}'] evaluates whether the name element is equal to the content of the second field.

The following XML data will be used for subsequent examples.

<?xml version="1.0"?>
	 
    <cricketers>
        <cricketer type="righty">
            <name>MS Dhoni</name>
            <role>Captain</role>
            <position>Wicket-Keeper</position>
            <salary>9</salary>
        </cricketer>
        <cricketer type="lefty">
            <name>Shikhar Dhawan</name>
            <role>              Batsman</role>
            <position>Point</position>
            <salary>10</salary>
        </cricketer>
        <cricketer type="righty">
            <name>Virat Kohli</name>
            <role>Batsman</role>
            <position>cover</position>
            <salary>11</salary>
        </cricketer>
        <cricketer type="righty">
            <name>Shami</name>
            <role>Bowler</role>
            <position>SquareLeg</position>
            <salary>12</salary>
        </cricketer>
        <cricketer type="lefty">
            <name>Zaheer Khan</name>
            <role>Bowler</role>
            <position>FineLeg</position>
            <salary>13</salary>
        </cricketer>
    </cricketers>


Examples:



1. Retrieve the content of the name field where the type attribute is equal to righty.

XPath Expression ://cricketer[@type='righty']/name/text()
XPath Type : XPathConstants.STRING
List's Value Delimiter :

result : Shikhar Dhawan


2. Concatenate the values of the type attributes for cricketers with the names Shami and Zaheer Khan.

XPath Expression :concat(//cricketer[name='Shami']/@type,'-',//cricketer[name='Zaheer Khan']/@type)
XPath Type : XPathConstants.STRING
List's Value Delimiter :

result : righty-lefty

3. Retrieve the values of the name field for all cricketers.

XPath Expression ://cricketers/cricketer/name/text()
XPath Type : XPathConstants.NODESET
List's Value Delimiter : ;

result : MS Dhoni;Shikhar Dhawan;Virat Kohli;Shami;Zaheer Khan

4. Retrieve the values of the name field for all cricketers where the type attribute is righty.

XPath Expression ://cricketers/cricketer[@type='righty']/name/text()
XPath Type : XPathConstants.NODESET
List's Value Delimiter : ;

result : MS Dhoni;Virat Kohli;Shami

5. Retrieve the position of the cricketer with the name “M S Dhoni”.

XPath Expression ://cricketers/cricketer[name='MS Dhoni']/position/text()
XPath Type : XPathConstants.STRING
List's Value Delimiter :

result : Wicket-Keeper

6. Retrieve the values of the type attributes for all cricketers.

XPath Expression ://cricketers/cricketer//@type
XPath Type : XPathConstants.NODESET
List's Value Delimiter : ;

result : righty;lefty;righty;righty;lefty


7. Retrieve the type attribute value of the cricketer with the name Zaheer Khan.

XPath Expression ://cricketers/cricketer[name='Zaheer Khan']/@type
XPath Type : XPathConstants.NODESET
List's Value Delimiter : ;

result : lefty

8. Retrieve information for all cricketers except the salary field, using the concat function on the nodeset.

XPath Expression : concat(/cricketers/cricketer/@type,':',/cricketers/cricketer/name/text(),',',/cricketers/cricketer/role/text(),',',/cricketers/cricketer/position/text())
XPath Type : XPathConstants.NODESET
List's Value Delimiter : ;

result :
righty:MS Dhoni,Captain,Wicket-Keeper;lefty:Shikhar Dhawan,              Batsman,Point;righty:Virat Kohli,Batsman,cover;righty:Shami,Bowler,SquareLeg;lefty:Zaheer Khan,Bowler,FineLeg


The preceding expression can be replaced with the following expression:


XPath Expression : concat(parent=/cricketers/cricketer # childs=/@type,/name/text(),/role/text(),/position/text() #separator=,)
XPath Type : XPathConstants.NODESET
List's Value Delimiter : ;

result :
righty,MS Dhoni,Captain,Wicket-Keeper;lefty,Shikhar Dhawan,              Batsman,Point;righty,Virat Kohli,Batsman,cover;righty,Shami,Bowler,SquareLeg;lefty:Zaheer Khan,Bowler,FineLeg

A list of useful paths and expression to select any node/ list of nodes from an XML document:

No Expression Description
1) node-name It is used to select all nodes with the given name "nodename"
2) / It specifies that selection starts from the root node.
3) // It specifies that selection starts from the current node that match the selection.
4) . Select the current node.
5) .. Select the parent of the current node.
6) @ Selects attributes.
7) cricketer Example - selects all nodes with the name "cricketer".
8) cricketers/cricketer Example - selects all cricketer elements that are children of cricketers
9) //cricketer Selects all cricketer elements no matter where they are in the document


Predicates

Predicates are used to find a specific node or a node that contains a specific value.

Predicates are always embedded in square brackets.

In the table below we have listed some path expressions with predicates and the result of the expressions:

Path Expression Result
/cricketers/cricketer[1] Selects the first cricketer element that is the child of the cricketers element.
/cricketers/cricketer[last()] Selects the last cricketer element that is the child of the cricketers element
/cricketers/cricketer[last()-1] Selects the last but one cricketer element that is the child of the cricketers element
/cricketers/cricketer[position()<3] Selects the first two cricketer elements that are children of the cricketers element
//cricketer[@type] Selects all the cricketer elements that have an attribute named type
//cricketer[@type='lefty'] Selects all the cricketer elements that have a "type" attribute with a value of "lefty"
/cricketers/cricketer[salary>10] Selects all the cricketer elements of the cricketers element that have a salary element with a value greater than 10
/cricketers/cricketer[salary>10]/name Selects all the name elements of the cricketer elements of the cricketers element that have a salary element with a value greater than 10