如何移除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脚本如下:
- 在根元素
soapenv:Envelope上统一声明所有需要的命名空间,避免子元素重复输出命名空间声明; - 根据期望输出调整命名空间前缀映射(
ind→typ,att→pur); - 修正脚本中的拼写错误和内容细节,匹配期望输出。
修改后的脚本:
%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
相关产品推荐
相关产品推荐

