如何在响应xml中声明名称空间

时间:2015-09-02 00:02:49

标签: mule mule-studio mule-component mule-el dataweave

我在Data Weaver中做POC。我不确定如何在整个输出xml中声明名称空间ns0:。参考链接:https://developer.mulesoft.com/docs/dataweave(我已经看到了使用命名空间1.4.3处理输入xml的示例,但我正在寻找输出xml)。

输入Xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <Records>
      <storenumber>12</storenumber>
      <calculated>false</calculated>
      <subTotal>12</subTotal>
   <Records>

预期输出xml:

<?xml version='1.0' eencoding="UTF-8" standalone="yes"?>
 <ns0:orders 
       xmlns="ns2=urn:test.com:common:v1">
  <ns0:order>
    <ns0:StoreID>12</ns0:StoreID>
    <ns0:Total>false</ns0:Total>
   </ns0:order>
  </ns0:orders>

DataWeaver转换

%dw 1.0
 %output application/xml
 %var baseurl="ns2=urn:test.com:common:v1"
 ---
 orders @(xmlns:baseurl): {
 order: {
  StoreID: payload.Records.storenumber,
  Total: payload.Records.calculated
  }
}

我相信转化中的@(xmlns:baseurl)是正确的。有没有其他方法来定义它?

1 个答案:

答案 0 :(得分:5)

使用指令%namespace <prefix> <uri>来声明命名空间 您可以使用<prefix>#<element>

指定元素的命名空间

DataWeave转换

%dw 1.0
%output application/xml
%namespace ns0 urn:test.com:common:v1
---
ns0#orders: {
  ns0#order: {
    ns0#StoreID: payload.Records.storenumber,
    ns0#Total: payload.Records.calculated
  }
}

输出

<?xml version='1.0' encoding='UTF-8'?>
<ns0:orders xmlns:ns0="urn:test.com:common:v1">
  <ns0:order>
    <ns0:StoreID>12</ns0:StoreID>
    <ns0:Total>false</ns0:Total>
  </ns0:order>
</ns0:orders>