xmlxpathssisxmltask

How to get an XML node using a SSIS XPATH XML task


I am trying to use a XML task in SSIS to get a session ID value from this XML:

<?xml version="1.0" encoding="utf-16"?>
<AuthenticationResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <authenticated xmlns="[link]">true</authenticated>
    <sessionId xmlns="[link]">0000-0000000-00000000000-000000</sessionId>
    <loginid xmlns="[link]">000000</loginid>
    <login_name xmlns="[link]">[Username]</login_name>
    <user_nbr xmlns="[link]">0000</user_nbr>
    <partition_id xmlns="[link]">3</partition_id>
    <is_parent_mail_user xmlns="[link]">0</is_parent_mail_user>
    <parent_user_id xmlns="[link]" />
    <user_id xmlns="[link]">[username]</user_id>
</AuthenticationResult>

However I cant get the task to return the Session ID

Task setup:

Operation Type: XPATH
SourceType: File connection
Source: testfile.xml

SaveOperationResult: True
DestinationType: Variable
Destination: User::SessionID
OverwriteDestination: True

SecondOperandType: Direct input
SecondOperand: //sessionId

Namespaces: (Collection)
PutResultsInOneNode: False
XPathOperation: Values

No name spaces are specified.

I have googled all around but I can't figure out why it is not working...


Solution

  • You can look at this answer for suggestions with XPath syntax. Basically you can make use of local-name() to read the element names without the namespace. You should be able to extract this session Id value with:

     <local name="sessionId"/>
     <xmltask source="${xml.file}">
        <copy path="/*[local-name()='AuthenticationResult']/*[local-name()='sessionId']/text()"
              property="sessionId"/>
     </xmltask>
     <echo message="sessionId = ${sessionId}"/>
    

    Alternatively, the simplified syntax below might work:

     <xmltask source="${xml.file}">
        <copy path="//:AuthenticationResult/:sessionId/text()"
              property="sessionId"/>
     </xmltask>