Search code examples
muledataweave

Access embedded XML attribute using Dataweave


I have this incoming payload

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <s:Header>
        <h:SendStreamRequestTrackingID xmlns:h="http://CargoWise.com/eHub/2010/06">601f0ce7-7d29-4677-83db-7d801559c595</h:SendStreamRequestTrackingID>
        <o:Security s:mustUnderstand="1"
            xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <u:Timestamp u:Id="_0">
                <u:Created>2022-09-21T03:25:38.454Z</u:Created>
                <u:Expires>2022-09-21T03:30:38.454Z</u:Expires>
            </u:Timestamp>
            <o:UsernameToken u:Id="uuid-9865f472-19ca-4d23-a4b8-3a7141fe1e57-19125">
                <o:Username>MyUsername</o:Username>
                <o:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">MyPassword</o:Password>
            </o:UsernameToken>
        </o:Security>
    </s:Header>
    <s:Body>
        <SendStreamRequest xmlns="http://CargoWise.com/eHub/2010/06">
            <Payload>
                <Message ApplicationCode="UDM" ClientID="BIZTALK" TrackingID="6c1c47de-3962-4cde-8b3f-6e9f245b1fae" SchemaName="http://www.cargowise.com/Schemas/Universal" SchemaType="Xml" EmailSubject="" FileName="">MyMessage</Message>
            </Payload>
        </SendStreamRequest>
    </s:Body>
</s:Envelope>

I have the following dataweave script

%dw 2.0
output application/xml writeDeclaration=false
ns s http://schemas.xmlsoap.org/soap/envelope/
ns u http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd
ns h http://CargoWise.com/eHub/2010/06
ns o http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd
---
s#envelope @('xmlns:u':u): payload.*s#Envelope map{
    s#Header: $.*s#Header map {
       h#SendStreamRequestTrackingID : $.h#SendStreamRequestTrackingID,
       o#Security @('s:mustUnderstand':"1" ): $.*o#Security map {
           o#UsernameToken @('u:Id':$.s#Header.o#Security.o#UsernameToken.@Id): $.UsernameToken
       }
    },
    s#body: $.s#Body
}

Which outputs the following message

<s:envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
  <s:Header>
    <h:SendStreamRequestTrackingID xmlns:h="http://CargoWise.com/eHub/2010/06">601f0ce7-7d29-4677-83db-7d801559c595</h:SendStreamRequestTrackingID>
    <o:Security xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" s:mustUnderstand="1">
      <o:UsernameToken u:Id="null">
        <o:Username>MyUsername</o:Username>
        <o:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">MyPassword</o:Password>
      </o:UsernameToken>
    </o:Security>
  </s:Header>
  <s:body>
    <SendStreamRequest xmlns="http://CargoWise.com/eHub/2010/06">
      <Payload>
        <Message ApplicationCode="UDM" ClientID="BIZTALK" TrackingID="6c1c47de-3962-4cde-8b3f-6e9f245b1fae" SchemaName="http://www.cargowise.com/Schemas/Universal" SchemaType="Xml" EmailSubject="" FileName="">MyMessage</Message>
      </Payload>
    </SendStreamRequest>
  </s:body>
</s:envelope>

You will notice that the incoming payload has this line

<o:UsernameToken u:Id="uuid-9865f472-19ca-4d23-a4b8-3a7141fe1e57-19125">

Whereas the outgoing message has this line

<o:UsernameToken u:Id="null">

I would like to know how to get the u:Id value into the output.

Many thanks


Solution

  • You just need to remove s#Header.o#Security because the path to that is already covered before the map function. So your usernameToken mapping and should look lile this

    o#UsernameToken @('u:Id':$.o#UsernameToken.@Id): $.UsernameToken