Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Saturday, August 8, 2015

Useful XPath expressions

The following WSO2 ESB proxy service creates a sample XML payload and logs some useful XPath expressions


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse" name="xpathSample" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
   <target>
      <inSequence>
         <payloadFactory media-type="xml">
            <format>
               <students xmlns="">
                  <student student-id="001">
                     <name first="tom" last="hanks" />
                     <age>30</age>
                     <scores>
                        <subject id="sub1">70</subject>
                        <subject id="sub2">80</subject>
                     </scores>
                  </student>
                  <student student-id="002">
                     <name first="tom" last="cruise" />
                     <age>25</age>
                     <scores>
                        <subject id="sub1">60</subject>
                        <subject id="sub2">75</subject>
                     </scores>
                  </student>
                  <student student-id="003">
                     <name first="jessica" last="chastain" />
                     <age>24</age>
                     <scores>
                        <subject id="sub1">75</subject>
                        <subject id="sub2">70</subject>
                     </scores>
                  </student>
               </students>
            </format>
            <args />
         </payloadFactory>
         <log level="custom" separator="&#xA;">
            <property name="student 001's first name" expression="//student[@student-id='001']/name/@first" />
            <property name="student 001's last name" expression="//student[@student-id='001']/name/@last" />
            <property name="student 001's age" expression="//student[@student-id='001']/age" />
            <property name="student 001's sub1 score" expression="//student[@student-id='001']/scores/subject[@id='sub1']/text()" />
            <property name="student 001's sub2 score" expression="//student[@student-id='001']/scores/subject[@id='sub2']/text()" />
            <property name="sub2 score for students whose first name is 'tom'" expression="//student[name[@first='tom']]/scores/subject[@id='sub2']" />
            <property name="sub2 score for students whose first name is 'tom' [with XPath2.0 function]" expression="string-join(//student[name[@first='tom']]/scores/subject[@id='sub2']/text(), ', ')" />
            <property name="students' last name whose age is lesser than '30'" expression="string-join(//student[age/text() &lt; 30]/name/@last, ', ')" />
            <property name="students' last name whose age is greater than '25' OR sub1 score is greater than or equal to '70'" expression="string-join(//student[age/text() /text() &gt; 25 or scores/subject[@id='sub1'] &gt;= 70]/name/@last, ', ')" />
         </log>
         <respond />
      </inSequence>
   </target>
   <description />
</proxy>

once the proxy is invoked using tryit or soapUI you can see the following logs in the terminal
student 001's first name = tom
student 001's last name = hanks
student 001's age = 30
student 001's sub1 score = 70
student 001's sub2 score = 80
sub2 score for students whose first name is 'tom' = 8075
sub2 score for students whose first name is 'tom' [with XPath2.0 function] = 80, 75
students' last name whose age is lesser than '30' = cruise, chastain
students' last name whose age is greater than '25' OR sub1 score is greater than or equal to 70 = hanks, chastain


Please note that, in Line 43, 44 and 45 I am using stirng-join(..) function to concat the results with comma. This function is only supported in XPath 2.0 version. But WSO2 ESB supports XPath 1.0 by default. To enable XPath 2.0 support in WSO2 ESB following line should be uncommented in CARON_HOME/repository/conf/synapse.properties
synapse.xpath.dom.failover.enabled=true