1

I want to use the following xpath

/Users/User/UserID

This does not work because the ESB adds a soap envelope and body around my xml, what is the correct xpath to use or how can I remove the soap envelope and body?

The xml is:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <Users xmlns="http://ws.wso2.org/dataservice">
         <User>
            <UserID>a</UserID>
            <Username>a</Username>
            <Email>a</Email>
            <Password>a</Password>
         </User>
         <User>
            <UserID>a</UserID>
            <Username>a</Username>
            <Email>a</Email>
            <Password>a</Password>
         </User>
         <User>
            <UserID>a</UserID>
            <Username>a</Username>
            <Email>a</Email>
            <Password>a</Password>
         </User>
      </Users>
   </soapenv:Body>
</soapenv:Envelope>

EDIT:

This works when I try to log it outside of my iterate mediator

//*[local-name() = 'Users']/*[local-name() = 'User']/*[local-name() = 'UserID']

but when I try to log it inside the iterate mediator it returns nothing?

Community
  • 1
  • 1
user3758298
  • 380
  • 5
  • 17

3 Answers3

1

Got this working by using the following

<property xmlns:int="http://ws.wso2.org/dataservice" name="uri.var.ID" expression="$body/int:User/int:UserID/text()" scope="default" type="STRING"/>
user3758298
  • 380
  • 5
  • 17
  • 'int' will likely be the namespace provided by data services. i.e. `xmlns:int="http://ws.wso2.org/dataservice"`. You can add namespaces in the UI when defining your property, they're often needed in WSO2 for XPATH expressions to work properly. – Strainy Dec 28 '15 at 07:57
  • Thank you for the great answer. – Victor Viola Feb 24 '16 at 19:51
0

To access your elements you should go through envelope and body. Here is an example with switch mediator:

<switch xmlns:ns="http://org.apache.synapse/xsd" 
        xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:int="http://ws.wso2.org/dataservice" 
        source="boolean(/soap:Envelope/soap:Body/int:Users/int:User/int:UserID/text() = 'string_to_compare'">
  <case regex="true">
     ....
  </case>
  <default/>
</switch>

UPD corrected XPath expression

user3714601
  • 1,156
  • 9
  • 27
  • Using the following returns nothing – user3758298 Jul 31 '14 at 07:17
  • It seems I missed to add namespace of Users node. Just updated the answer, can you try it? – user3714601 Jul 31 '14 at 08:38
  • That still returns nothing – user3758298 Jul 31 '14 at 09:09
  • Can you try "/soap:Envelope/soap:Body/int:Users/int:User/int:UserID" ? – user3714601 Jul 31 '14 at 10:21
  • I tried that and that also doesn't work, I found this online and this seems to work //*[local-name() = 'UserID'] , will using this cause any problems though? – user3758298 Jul 31 '14 at 10:34
  • This will match UserID node in amy namespace anywhere in the document. So it looks a bit risky. But you can try to construct the whole chain with wildcarded namespaces: /*[local-name() = 'Envelope']/*[local-name() = 'Body']/*[local-name() = 'Users']/*[local-name() = 'User']/*[local-name() = 'UserID'] – user3714601 Jul 31 '14 at 10:42
  • I'd say that probably your example XML was not completely correct, some nodes holding a different namespace or whatsoever. Because /soap:Envelope/soap:Body/int:Users/int:User/int:UserID must match nodes from your example. – user3714601 Jul 31 '14 at 10:49
  • Using /*[local-name() = 'Envelope']/*[local-name() = 'Body']/*[local-name() = 'Users']/*[local-name() = 'User']/*[local-name() = 'UserID'] again returns nothing, the xml I posted in my question is what gets returned if I log just "/" – user3758298 Jul 31 '14 at 10:53
  • Using this //*[local-name() = 'Users']/*[local-name() = 'User']/*[local-name() = 'UserID'] I'm able to get the result back, also using that I can use the iterate mediator to iterate over each value. My problem now is if I try to log that inside the iterate mediator it returns nothing? – user3758298 Jul 31 '14 at 12:47
  • I have now got this working using the following – user3758298 Aug 01 '14 at 13:35
0

Try this:

<property name="yourParamName" expression="$body/Users/User/UserID" scope="default" type="STRING"/>

You can read more on the predefined Synapse XPath variables here.

Voicu
  • 16,921
  • 10
  • 60
  • 69