2017-02-15 3 views
0

Ich habe Angenommen XMLs wieTransformation von XML mit Standardwerten in MuleSoft

<a> 
    <b>Some Value</b> 
    </a> 

    ...or... 
    <a> 
    <b type=1 /> 
    </a> 

    ...or... 
    <a/> 

und will einige Werte und Attribute in der Ausgabe definiert haben, wie

<a> 
    <b type=0>Some Value</b> 
    </a> 

    ...or... 
    <a> 
    <b type=1>Empty</b> 
    </a> 

    ...or... 
    <a> 
    <b type=0>Empty</b> 
    </a> 

was bester Weg wäre, dies zu tun in Mulesoft?

Mit Skript mit Zeilen wie

if (payload.a == null) payload.a={} 
if (payload['a']['b']) payload['a']['b']={} 
if (payload.a.b.type == null) payload.a.b.type=0; 

oder dataweave

%dw 1.0 
%output application/xml 
--- 
{ 
    a: payload.a default { {b:{ [email protected]=0 }} } 
} 

Ich bin verwirrt über hier Syntax.

Antwort

2

Wenn ich es richtig verstehe, was Sie fragen, scheint folgendes zu arbeiten:

Eingang:

<?xml version='1.0' encoding='UTF-8'?> 
<root> 
    <a> 
    <b>Some Value</b> 
    </a> 
    <a> 
    <b type="1" /> 
    </a> 
    <a/> 
</root> 

Dataweave:

%dw 1.0 
%output application/xml 
--- 
root: payload.root.*a mapObject (
    a: { 
     b @(type: [email protected] default "0"): 
      $.b when $.b != null and $.b != "" otherwise "Empty" 
    } 
) 

Ausgabe:

<?xml version='1.0' encoding='UTF-8'?> 
<root> 
    <a> 
    <b type="0">Some Value</b> 
    </a> 
    <a> 
    <b type="1">Empty</b> 
    </a> 
    <a> 
    <b type="0">Empty</b> 
    </a> 
</root> 
+0

Ja, irgendwie ... Danke für die Syntax - es sollte funktionieren ... für diese extrem einfache Struktur. Ich habe Rat gesucht, wie man es einfach macht. Stellen Sie sich echtes xml mit Hunderten von Feldern vor ... Wenn niemand einen besseren Vorschlag macht, werde ich Ihre Antwort später akzeptieren ... – Alex