Pages

Wednesday, September 14, 2016

Basics of XPath used in SOA

XPath is used to navigate through an XML document. It has some XPath predefined functions owned by W3schools to fetch parts of the XML document. On top of these functions Oracle SOA has defined some custom functions to suite Oracle SOA requirements. In this document we will discuss the basic XPath functions used is Oracle SOA.


Consider below XML document that can be sent to a SOA webservice, and let us see some XPath expressions and their results.

The root element in this document is <inputVar> which is represented by “/” in XPath.
1. To get order number:
Expression: /ns:orderDetails/ns:orderHeader/ns:orderNumber
Oracle SOA returns: ORD1231 OR <ns:orderNumber>ORD1231</ns:orderNumber>

Expression: /ns:orderDetails/ns:orderHeader/ns:orderNumber/text()
Result: ORD1231

2. To get order line:
Expression:  /ns:orderDetails/ns:orderHeader/ol:orderLine
Result:      <ol:orderLine lineIdentifier="1231_11">
               <ol:item>Hard Disk</ol:item>
               <ol:unitPrice>TS</ol:unitPrice>
             </ol:orderLine>

3. To get lineIdentifier:
Expression: /ns:orderDetails/ns:orderHeader/ol:orderLine/@lineIdentifier
Result:     1234_11


Below is another XML document which has more hierarchal structure. We will see how to deal with fetching first and last elements/nodes and element at a given index.


1. To get the first order orderNumber:
   Possible expressions:
        /ns:orderDetails/ns:orderHeader[position()=1]/ns:orderNumber
     /ns:orderDetails/ns:orderHeader[position()=first()]/ns:orderNumber
        /ns:orderDetails/ns:orderHeader[first()]/ns:orderNumber
        /ns:orderDetails/ns:orderHeader[1]/ns:orderNumber
   Result: <ns:orderNumber>ORD1231</ns:orderNumber>    OR    ORD1231

2. To get last order unitPrice:
   Possibel expressions:
/ns:orderDetails/ns:orderHeader[position()=last()]/ol:orderLine/ol:unitPrice
/ns:orderDetails/ns:orderHeader[last()]/ol:orderLine/ol:unitPrice
/ns:orderDetails/ns:orderHeader/ol:orderLine[../last()]/ol:unitPrice
   Result: <ol:unitPrice>12452</ol:unitPrice>   OR  12452

3. To get orderNumber whoes orderId is 1232:
Expression: /ns:orderDetails/ns:orderHeader[ns:orderId=1232]/ns:orderNumber
Result: <ns:orderNumber>ORD1232</ns:orderNumber>    OR ORD1232

4. To get orderId where item is MONITOR:
/ns:orderDetails/ns:orderHeader[ol:orderLine/ol:item="MONITOR"]/ns:orderId
result : <ns:orderId>1233</ns:orderId>  OR  1233

5. To get unitPrice of orderNumber ORD1232
Expression:
/ns:orderDetails/ns:orderHeader[ns:orderNumber="ORD1232"]/ol:orderLine/ol:unitPrice
/ns:orderDetails/ns:orderHeader/ol:orderLine[../orderNumber="ORD1232"]/ol:unitPrice
Result: 312

6. To find number of orderHeaders in xml.
Expression: count(/ns:orderDetails/ns:orderHeader)
Result: 3

7. To get orderNumber where lineIdentifier is 1232_12
Expression: /ns:orderDetails/ns:orderHeader[ol:orderLine/@lineIdentifier=1232_12]/ns:orderNumber
Result: <ns:orderNumber>ORD1232</ns:orderNumber>   OR    ORD1232

8. To get orderNumber based on runtime position value.
Expression:
   /ns:orderDetails/ns:orderHeader[position()=$indexVar]/ns:orderNumber
        /ns:orderDetails/ns:orderHeader[$indexVar]/ns:orderNumber
        Heare indexVar is index variable.
Result: orderNumber of order at index indexVar
Above discussed expression cover most of the Oracle SOA project requirements. There are additional conditional expressions where you can use logical operators and functions.

No comments:

Post a Comment