我必须从 XML 负载中删除一些字段。我在下面写了一个函数,但它只检查并删除键。
如何将条件也应用于属性和值?
基本上,它已经删除了任何可用的字段,如标签/值/属性
样本输入
<soapenv:Envelope xmlns:soapenv=http://www.w3.org/2003/05/soap-envelope>
<soapenv:Header>
<axis2ns11343:OSARequestHeader xmlns:axis2ns11343=http://www.host.com/xmlschema/resource/metadata/osa/infrastructure/v001 soapenv:mustUnderstand="false">
<ns5:userId xmlns:ns5=http://www.host.com/xmlschema/resource/metadata/osa/infrastructure/v001>e2eauto01a</ns5:userId>
<ns5:companyId xmlns:ns5=http://www.host.com/xmlschema/resource/metadata/osa/infrastructure/v001>test</ns5:companyId>
<ns5:companyType xmlns:ns5=http://www.host.com/xmlschema/resource/metadata/osa/infrastructure/v001/>
<ns5:clientMachineId xmlns:ns5=http://www.host.com/xmlschema/resource/metadata/osa/infrastructure/v001>1.1.1.1</ns5:clientMachineId>
</axis2ns11343:OSARequestHeader>
<axis2ns11344:cpsMessageHeader xmlns:axis2ns11344=http://host.com/xsd/types/V002 guid="28cd611f-2e08-4650-8217-cdd27284968f" soapenv:mustUnderstand="false" traceId="28cd611f-2e08-4650-8217-cdd27284968f"/>
</soapenv:Header>
<soapenv:Body>
<ns8:getCurrencyProfileRequest xmlns:ns8=http://host.com/xsd/service/V001 username="username" type="account" xmlns:ns3=http://host.com/xsd/commontypes/V001 xmlns:ns4=http://host.com/xsd/types/V002 xmlns:ns5=http://www.host.com/xmlschema/resource/metadata/osa/infrastructure/v001 xmlns:ns6=http://host.com/xsd/paymentorchestrator/sla/V001 xmlns:ns7=http://host.com/xsd/fxservice/tradeService/V001>
<ns8:userInfo>
<ns3:type>accountNo</ns3:type>
<ns3:companyId>string</ns3:companyId>
<ns3:userId>string</ns3:userId>
<ns3:accountNo>string</ns3:accountNo>
<ns3:txid>string</ns3:txid>
</ns8:userInfo>
</ns8:getCurrencyProfileRequest>
</soapenv:Body>
</soapenv:Envelope>
样本输出
<soapenv:Envelope xmlns:soapenv=http://www.w3.org/2003/05/soap-envelope>
<soapenv:Header>
<axis2ns11343:OSARequestHeader xmlns:axis2ns11343=http://www.host.com/xmlschema/resource/metadata/osa/infrastructure/v001 soapenv:mustUnderstand="false">
<ns5:userId xmlns:ns5=http://www.host.com/xmlschema/resource/metadata/osa/infrastructure/v001>e2eauto01a</ns5:userId>
<ns5:companyId xmlns:ns5=http://www.host.com/xmlschema/resource/metadata/osa/infrastructure/v001>test</ns5:companyId>
<ns5:companyType xmlns:ns5=http://www.host.com/xmlschema/resource/metadata/osa/infrastructure/v001/>
<ns5:clientMachineId xmlns:ns5=http://www.host.com/xmlschema/resource/metadata/osa/infrastructure/v001>1.1.1.1</ns5:clientMachineId>
</axis2ns11343:OSARequestHeader>
<axis2ns11344:cpsMessageHeader xmlns:axis2ns11344=http://host.com/xsd/types/V002 guid="28cd611f-2e08-4650-8217-cdd27284968f" soapenv:mustUnderstand="false" traceId="28cd611f-2e08-4650-8217-cdd27284968f"/>
</soapenv:Header>
<soapenv:Body>
<ns8:getCurrencyProfileRequest xmlns:ns8=http://host.com/xsd/service/V001 xmlns:ns3=http://host.com/xsd/commontypes/V001 xmlns:ns4=http://host.com/xsd/types/V002 xmlns:ns5=http://www.host.com/xmlschema/resource/metadata/osa/infrastructure/v001 xmlns:ns6=http://host.com/xsd/paymentorchestrator/sla/V001 xmlns:ns7=http://host.com/xsd/fxservice/tradeService/V001>
<ns8:userInfo>
<ns3:txid>string</ns3:txid>
</ns8:userInfo>
</ns8:getCurrencyProfileRequest>
</soapenv:Body>
</soapenv:Envelope>
%dw 1.0
%output application/xml skipNullOn="everywhere"
%function remove(content, filterList)
content match {
:array -> $ map (value,index) -> remove(value, filterList),
:object -> $ mapObject (value,key) ->
(key) @((key.@)) : null when (filterList contains key as :string)
otherwise remove(value, filterList)
,
default -> content
}
---
remove(payload, ["userId","companyId","account"])
输入不是有效的 XML,我已更新以使其有效。下面的 Dataweave 删除所有匹配过滤器列表的属性名称/值和标签/值。
注意在输出属性中
type
被移除因为值匹配account
输入
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Header>
<axis2ns11343:OSARequestHeader xmlns:axis2ns11343="http://www.host.com/xmlschema/resource/metadata/osa/infrastructure/v001" soapenv:mustUnderstand="false">
<axis2ns11343:userId>e2eauto01a</axis2ns11343:userId>
<axis2ns11343:companyId>test</axis2ns11343:companyId>
<axis2ns11343:companyType/>
<axis2ns11343:clientMachineId>1.1.1.1</axis2ns11343:clientMachineId>
</axis2ns11343:OSARequestHeader>
<axis2ns11344:cpsMessageHeader xmlns:axis2ns11344="http://host.com/xsd/types/V002" guid="28cd611f-2e08-4650-8217-cdd27284968f" soapenv:mustUnderstand="false" traceId="28cd611f-2e08-4650-8217-cdd27284968f"/>
</soapenv:Header>
<soapenv:Body>
<ns8:getCurrencyProfileRequest xmlns:ns8="http://host.com/xsd/service/V001" username="username" type="account">
<ns8:userInfo>
<ns3:type xmlns:ns3="http://host.com/xsd/commontypes/V001">accountNo</ns3:type>
<ns3:companyId xmlns:ns3="http://host.com/xsd/commontypes/V001">string</ns3:companyId>
<ns3:userId xmlns:ns3="http://host.com/xsd/commontypes/V001">string</ns3:userId>
<ns3:accountNo xmlns:ns3="http://host.com/xsd/commontypes/V001">string</ns3:accountNo>
<ns3:txid xmlns:ns3="http://host.com/xsd/commontypes/V001">string</ns3:txid>
</ns8:userInfo>
</ns8:getCurrencyProfileRequest>
</soapenv:Body>
</soapenv:Envelope>
%dw 1.0
%output application/xml skipNullOn="everywhere"
%function checkAndRemoveAttributes (attribs, filterList)
(attribs default {}) mapObject {
($$): null when ((filterList contains $$) or (filterList contains ($ as :string))) otherwise $
}
%function remove(content, filterList)
content match {
:object -> $ mapObject (value,key) ->
(key) @((checkAndRemoveAttributes(key.@,filterList))): null when (filterList contains key as :string)
otherwise remove(value, filterList)
,
default -> null when (filterList contains content as :string) otherwise content
}
---
remove(payload, ["userId","companyId","account","username"])
输出
<?xml version='1.0' encoding='US-ASCII'?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Header>
<axis2ns11343:OSARequestHeader xmlns:axis2ns11343="http://www.host.com/xmlschema/resource/metadata/osa/infrastructure/v001" soapenv:mustUnderstand="false">
<axis2ns11343:companyType></axis2ns11343:companyType>
<axis2ns11343:clientMachineId>1.1.1.1</axis2ns11343:clientMachineId>
</axis2ns11343:OSARequestHeader>
<axis2ns11344:cpsMessageHeader xmlns:axis2ns11344="http://host.com/xsd/types/V002" guid="28cd611f-2e08-4650-8217-cdd27284968f" soapenv:mustUnderstand="false" traceId="28cd611f-2e08-4650-8217-cdd27284968f"></axis2ns11344:cpsMessageHeader>
</soapenv:Header>
<soapenv:Body>
<ns8:getCurrencyProfileRequest xmlns:ns8="http://host.com/xsd/service/V001">
<ns8:userInfo>
<ns3:type xmlns:ns3="http://host.com/xsd/commontypes/V001">accountNo</ns3:type>
<ns3:accountNo xmlns:ns3="http://host.com/xsd/commontypes/V001">string</ns3:accountNo>
<ns3:txid xmlns:ns3="http://host.com/xsd/commontypes/V001">string</ns3:txid>
</ns8:userInfo>
</ns8:getCurrencyProfileRequest>
</soapenv:Body>
</soapenv:Envelope>