Vishal Dattaram Patil
Vishal Dattaram Patil

Reputation: 21

How to use tokenize function in Xpath

can we use tokenize function in XPath

The general java code i use to process XSLT and XML files are :

XPath xPath = XPathFactory.newInstance().newXPath();
InputSource inputXML = new InputSource(new StringReader(xml));
String expression = "/root/customer/personalDetails[age=tokenize('20|30','|')]/name";
boolean evaluate1 = (boolean) xPath.compile(expression).evaluate(inputXML, XPathConstants.BOOLEAN);

XML :-

<?xml version="1.0" encoding="ISO-8859-15"?>
<root>
    <customer>
        <personalDetails>
            <name>ABC</name>
            <value>20</value>
        </personalDetails>
        <personalDetails>
            <name>XYZ</name>
            <value>21</value>
        </personalDetails>
        <personalDetails>
            <name>PQR</name>
            <value>30</value>
        </personalDetails>
    </customer>
</root>

Expected Response :- ABC,PQR

Upvotes: 2

Views: 647

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

Yes, you can use the tokenize() function in XPath, provided your XPath processor supports XPath 2.0 or later.

For Java, the popular choice of XPath 2.0+ processor is Saxon.

You can use the JAXP API with Saxon, however, it's not really designed to work well with XPath 2.0+, so it's preferable to use Saxon's own API (called s9api).

For this particular example, you don't need tokenize(). In XPath 2.0+ you can write

[age=('20', '30')]

Upvotes: 1

Related Questions