Introduction
In this post, you will learn how to extract a single XML node value from your XML web response (SOAP Web service call) using an XPath expression in the SSIS REST API Web Service Task and the XML Parser Task.
This article assumes you have basic knowledge of how to call a REST API or a SOAP Web service in SSIS. If you are not familiar with that, then refer to the 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 the SSIS REST API Task
Refer to the following video on how to call a REST API web service.
Extract the REST API Response into a file
Here is how you can save the response into a variable or a file. Go to the Response settings tab and select the check Response content option, as shown below. You can also Filter Response content (e.g., Extract only a single value from a node).
Extract a single XML Node using XPath (XML with Namespace)
To extract a single value from the Response XML, you can define an XPath expression on the REST API Response tab as follows. If your response XML document contains namespaces (e.g., the node looks like this: <ns1:Body xmlns:ns1=”http://abc.com”><ns1:Data>abcdefg</ns1:Data></ns1:Body>), then you have to write XPath in a particular way, as shown below. Notice we have not specified the 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:
The above expression will produce the following result
|
1 |
abcdefg |
Extract Attribute Value using XPath local-name() function
Now, let’s find out how to extract the attribute value rather than the element.
Consider the following XML. Let’s say we want to extract the id from the data node. In such a case, the previous XPath expression won’t work, so we need to use a 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 the XML Parser Task to test XPath.
Expression 1 – Get value by direct attribute name
Write the 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 the SSIS Rest API Task to filter XML Response using XPath
The screenshot below shows how to filter XML data using an XPath expression. You can save extracted data to an SSIS Variable or save it to a file.
More Examples of 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 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) s_1234 XPath Expression: //@*[local-name()='token'] Returns: 123456 ---------------------------- |







