Introduction
In this post you will learn how to extract single XML node value from your XML web response (SOAP Web service call) using XPath expression in SSIS REST API Web Service Task and e XML Parser Task.
This article assumes you have basic knowledge how to call REST API or SOAP Web service in SSIS. If you are not familiar with that then refer following articles.
Calling SOAP Web Service in SSIS (XML Source)
Call REST API using SSIS Web Service Task / JSON / XML Source
Calling REST / SOAP Web service using SSIS REST API Task
Refer following video how to call REST API web service.
Extract REST API Response into file
Here is how you can save response into variable or file. Go to Response settings tab and select check Response content option like below. You can also Filter Response content (e.g. Extract only single value from node).
Extract single XML Node using XPath (XML with Namespace)
To extract single value from Response XML you can define XPath expression on REST API Response tab as below. If your response XML document contains namespaces (e.g. node looks like this <ns1:Body xmlns:ns1=”http://abc.com”><ns1:Data>abcdefg</ns1:Data></ns1:Body> then you have to write XPath very specific way like below. Notice we have not specified Namespace prefix ns1
Sample XML Document (Or web response from SOAP Service)
1 2 3 |
<ns1:Body xmlns:ns1="http://abc.com"> <ns1:Data>abcdefg</ns1:Data> </ns1:Body> |
XPath Expression Example:
1 |
//*[local-name()='Data'] |
XPath Output:
Above expression will produce below result
1 |
abcdefg |
Extract Attribute Value using XPath local-name() function
Now lets find out how to extract attribute value rather than element.
Consider following XML. Let’s say we want to extract id from data node. In such case previous XPath expression won’t work so we need to use slightly different XPath.
1 2 3 |
<ns1:Body xmlns:ns1="http://abc.com"> <ns1:Data id="12345">abcdefg</ns1:Data> </ns1:Body> |
Let’s use XML Parser Task to test XPath.
Expression 1 – Get value by direct attribute name
Write following expression as below and click Test
1 |
//@*[local-name()='id'] |
Expression 2 – Get specific element attribute value
If you want to be specific and read the value of the id
attribute from a particular data
element, you can use an expression like this:
1 |
//*[local-name()='Data']/@*[local-name()='id'] |
Configure SSIS Rest API Task to filter XML Response using XPath
Below screenshot shows how to filter XML data using XPath expression. You can save extracted data to SSIS Variable or save to file.
More Examples of XPath expression
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 52 53 54 |
XPath expression examples: ============================ Example1: Extract value of any element by node name (without namespace specified) <ns1:AuthInfo><ns1:Token>aaaaa</ns1:Token></ns1:AuthInfo> XPath Expression: //*[local-name() = 'Token'] Returns: aaaaa ---------------------------- Example2: Extract text of element called Token <AuthInfo><Token>aaaaa</Token></AuthInfo> XPath Expression: ./AuthInfo/Token/text() Returns: aaaaa ---------------------------- Example3: Extract value of attribute called Version <Doc><Row Version="1" /></Doc> XPath Expression: ./Doc/Row/@Version Returns: 1 ---------------------------- Example4: Extract value of following-sibling node or use preceding-sibling <Response> <Prop> <PropName>User</PropName> <PropVal>admin</PropVal> </Prop> <Prop> <PropName>SessionId</PropName> <PropVal>s_123456</PropVal> </Prop> </Response> Expression: //*[local-name()='PropName' and text()='SessionID']/following-sibling::node()[local-name()='PropValue'] Returns: s_123456 ---------------------------- Example5: Extract node value with matching attribute <Response><Prop Name="SessionId">s_1234</Prop></Response> XPath Expression: //*[local-name()='Prop' and @Name='SessionId'] Returns: s_1234 ---------------------------- Example6: Extract attribute value by name (without namespace specified) <Response><Prop token="123456">s_1234</Prop></Response> XPath Expression: //@*[local-name()='token'] Returns: 123456 ---------------------------- |