You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除JSON转XML转换后自动添加的xmlns命名空间属性

JSON转XML时移除子元素命名空间声明问题

需求:将JSON转换为XML输出时,需要移除子元素上的xmlns:ind="."和xmlns:att="d"命名空间声明,同时匹配指定的期望输出格式。

示例输入

{
    "order": "Electronic",
    "productId": 6548790,
    "status": "COMPLETED",
    "recieved": "YES",
    "orderId": "12453",
    "desc": "NASQ123",
    "details": [{
        "attributes": [{
            "linetype": "DFU"
        }]  
    }]
}

使用的DataWeave脚本

%dw 2.0
output application/xml writeDeclaration=false
ns soapenv http://schemas.xmlsoap.org/soap/envelope/
ns ind .
ns att d
---
{
soapenv#Envelope:{
soapenv#Header: {},
soapenv#Body: {
ind#updateorder: {
ind#orderId: payload.orderId,
ind#orderName: payload.order,
ind#quantity: "5",
ind#order: {
att#productId: payload.productId,
att#desc: payload.desc,
att#shipping: "MAIL",
att#status: payload.status,
att#type1: payload.order,
att#recieved: payload.recieved,
att#otherDetails: payload. *details map ()-> {
att#details1: $.attributeS.linetype,
},
}
}
}
}
}

当前输出

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header/>
  <soapenv:Body>
    <ind:updateorder xmlns:ind=".">
      <ind:orderId>12453</ind:orderId>
      <ind:orderName>Electronic</ind:orderName>
      <ind:quantity>5</ind:quantity>
      <ind:order>
        <att:productId xmlns:att="d">6548790</att:productId>
        <att:desc xmlns:att="d">NASQ123</att:desc>
        <att:shipping xmlns:att="d">MAIL</att:shipping>
        <att:status xmlns:att="d">COMPLETED</att:status>
        <att:type1 xmlns:att="d">Electronic</att:type1>
        <att:recieved xmlns:att="d">YES</att:recieved>
        <att:otherDetails xmlns:att="d">
          <att:details1/>
        </att:otherDetails>
      </ind:order>
    </ind:updateorder>
  </soapenv:Body>
</soapenv:Envelope>

期望输出

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header/>
  <soapenv:Body>
    <typ:updateorder>
      <typ:orderId>12453</typ:orderId>
      <typ:orderName>Electronic</typ:orderName>
      <typ:quantity>5</typ:quantity>
      <typ:order>
        <pur:productId>6548790</pur:productId>
        <pur:desc>NASQ123</pur:desc>
        <pur:shipping>MAIL</pur:shipping>
        <pur:status>COMPLETED</pur:status>
        <pur:type1>NA</pur:type1>
        <pur:recieved>YES</pur:recieved>
        <pur:otherDetails>
          <pur:details1/>
        </pur:otherDetails>
      </typ:order>
    </typ:updateorder>
  </soapenv:Body>
</soapenv:Envelope>

解决方案

要达成需求,需调整DataWeave脚本如下:

  1. 在根元素soapenv:Envelope上统一声明所有需要的命名空间,避免子元素重复输出命名空间声明;
  2. 根据期望输出调整命名空间前缀映射(ind→typ,att→pur);
  3. 修正脚本中的拼写错误和内容细节,匹配期望输出。

修改后的脚本:

%dw 2.0
output application/xml writeDeclaration=false
ns soapenv http://schemas.xmlsoap.org/soap/envelope/
ns typ .
ns pur d
---
{
    soapenv#Envelope @(xmlns:typ=".", xmlns:pur="d"): {
        soapenv#Header: {},
        soapenv#Body: {
            typ#updateorder: {
                typ#orderId: payload.orderId,
                typ#orderName: payload.order,
                typ#quantity: "5",
                typ#order: {
                    pur#productId: payload.productId,
                    pur#desc: payload.desc,
                    pur#shipping: "MAIL",
                    pur#status: payload.status,
                    pur#type1: "NA",
                    pur#recieved: payload.recieved,
                    pur#otherDetails: payload.details map ()-> {
                        pur#details1: $.attributes.linetype
                    }
                }
            }
        }
    }
}

关键调整说明

  • 根元素通过@(xmlns:typ=".", xmlns:pur="d")统一声明命名空间,子元素无需重复输出xmlns属性;
  • 修正原脚本中attributeS的拼写错误为attributes,移除不必要的通配符*;
  • 将pur#type1的值改为"NA",匹配期望输出;
  • 前缀映射调整为typ和pur,与期望输出一致。

内容的提问来源于stack exchange,提问作者james11

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 02:06:05