December 16, 2016

Useful XPath

Join Element Text

Input :
<employees>
    <employee>
        <empId>123</empId>
    </employee>
    <employee>
        <empId>456</empId>
    </employee>
</employees>

XPath : string-join(//empId, ',')

Output :
123,456

Distinct Values in XPath 1.0
Distinct function is not available in XPath 2.0. Therefore in XPath 1.0 you have to use the following.

Input :
<students>
<student>
<studentId>100</studentId>
<subject>Maths</subject>
</student>
<student>
<studentId>102</studentId>
<subject>Science</subject>
</student>
<student>
<studentId>101</studentId>
<subject>English</subject>
</student>
<student>
<studentId>100</studentId>
<subject>English</subject>
</student>
<student>
<studentId>100</studentId>
<subject>Science</subject>
</student>
</students>

XPath : //student/studentId[not(.=preceding::*)]

Output :
<result>
   <studentId>100</studentId>
   <studentId>102</studentId>
   <studentId>101</studentId>
</result>

No comments:

Post a Comment