简体   繁体   中英

Mulesoft XML dataweave set namespace for attributes

I am trying to form a xml request which has some attributes. The attributes also has some namespace. Can someone help me with the correct syntax to form the correct xml request.

eg

     Sample xml

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:ns2="http://www.w3.org/2001/XMLSchema"
     <soapenv:Header/>
        <soapenv:Body>
        <Request>
        <soapenv:age ns1:type="ns2:string">1234</soapenv:age>
        </Request>
    </soapenv:Body>
    </soapenv:Envelope> 


    dataweave
    %dw 1.0
    %output application/xml
    {
     ns1#age @(ns1#type: "ns2#string"): "1234"
     }
%dw 1.0
%output application/xml encoding="utf-8"
%namespace soapenv http://schemas.xmlsoap.org/soap/envelope/
%namespace ns1 http://www.w3.org/2001/XMLSchema-instance
%namespace ns2 http://www.w3.org/2001/XMLSchema
---
soapenv#Envelope: {
  soapenv#Header  : {
      soapenv#Body: {
        Request: {
          soapenv#age @(ns1#type: "ns2#string"): "1234"
        }
    }
  }
}

Output:

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header>
    <soapenv:Body>
      <Request>
        <soapenv:age xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance" ns1:type="ns2#string">1234</soapenv:age>
      </Request>
    </soapenv:Body>
  </soapenv:Header>
</soapenv:Envelope>

Note that ns2 is not used by the transformation so it will not emitted in the output. If you need it to appear you'll need to add a dummy attribute to some element with ns2 as the namespace (example @(ns2#dummy:'') .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM