我試圖弄清楚如何在 dataweave 中做到這一點,但運氣不佳。
有沒有一種優雅的方法可以做到這一點而不必再次重建 XML 結構?
鑒于以下源和目標示例,您將如何將新元素僅注入第一個 UserArea/PropertyList?
新元素:
<Property>
<NameValue name="new.attribute">new value</NameValue>
</Property>
原始碼結構
<?xml version="1.0" encoding="UTF-8"?>
<root>
<UserArea>
<PropertyList>
<Property>
<NameValue name="xxx.CreatedBy">Test 1</NameValue>
</Property>
<Property>
<NameValue name="xxx.EnteredBy">Test 2</NameValue>
</Property>
<Property>
<NameValue name="xxx.SafetyFlag">false</NameValue>
</Property>
<Property>
<NameValue name="xxx.DependFlag">true</NameValue>
</Property>
<Property>
<NameValue name="eam.UDFCHAR10">ABC</NameValue>
</Property>
</PropertyList>
</UserArea>
<UserArea>
<PropertyList>
<Property>
<NameValue name="xxx.Exited">Test 3</NameValue>
</Property>
<Property>
<NameValue name="xxx.Entered">Test 4</NameValue>
</Property>
<Property>
<NameValue name="xxx.SafetyFlag">false</NameValue>
</Property>
<Property>
<NameValue name="xxx.DependFlag">true</NameValue>
</Property>
<Property>
<NameValue name="eam.UDFCHAR10">ABC</NameValue>
</Property>
</PropertyList>
</UserArea>
</root>
目標結構
<?xml version="1.0" encoding="UTF-8"?>
<root>
<UserArea>
<PropertyList>
<Property>
<NameValue name="xxx.CreatedBy">Test 1</NameValue>
</Property>
<Property>
<NameValue name="xxx.EnteredBy">Test 2</NameValue>
</Property>
<Property>
<NameValue name="xxx.SafetyFlag">false</NameValue>
</Property>
<Property>
<NameValue name="xxx.DependFlag">true</NameValue>
</Property>
<Property>
<NameValue name="xxx.UDFCHAR10">ABC</NameValue>
</Property>
<Property>
<NameValue name="new.attribute">new value</NameValue>
</Property>
</PropertyList>
</UserArea>
<UserArea>
<PropertyList>
<Property>
<NameValue name="xxx.Exited">Test 3</NameValue>
</Property>
<Property>
<NameValue name="xxx.Entered">Test 4</NameValue>
</Property>
<Property>
<NameValue name="xxx.SafetyFlag">false</NameValue>
</Property>
<Property>
<NameValue name="xxx.DependFlag">true</NameValue>
</Property>
<Property>
<NameValue name="xxx.UDFCHAR10">ABC</NameValue>
</Property>
</PropertyList>
</UserArea>
</root>
uj5u.com熱心網友回復:
你會認為這是重建 xml 結構還是你的意思是手工制作 DW 腳本中的每個元素
輸入 如問題中所述
腳本
%dw 2.0
output application/xml
var newElementAdded = {"NameValue" @(name:"new.attribute"): "new value"}
---
root: (payload.root mapObject {
UserArea: (if(($$$) == 0) "PropertyList": $.PropertyList {"Property": newElementAdded} else $)
})
輸出
<?xml version='1.0' encoding='UTF-8'?>
<root>
<UserArea>
<PropertyList>
<Property>
<NameValue name="xxx.CreatedBy">Test 1</NameValue>
</Property>
<Property>
<NameValue name="xxx.EnteredBy">Test 2</NameValue>
</Property>
<Property>
<NameValue name="xxx.SafetyFlag">false</NameValue>
</Property>
<Property>
<NameValue name="xxx.DependFlag">true</NameValue>
</Property>
<Property>
<NameValue name="eam.UDFCHAR10">ABC</NameValue>
</Property>
<Property>
<NameValue name="new.attribute">new value</NameValue>
</Property>
</PropertyList>
</UserArea>
<UserArea>
<PropertyList>
<Property>
<NameValue name="xxx.Exited">Test 3</NameValue>
</Property>
<Property>
<NameValue name="xxx.Entered">Test 4</NameValue>
</Property>
<Property>
<NameValue name="xxx.SafetyFlag">false</NameValue>
</Property>
<Property>
<NameValue name="xxx.DependFlag">true</NameValue>
</Property>
<Property>
<NameValue name="eam.UDFCHAR10">ABC</NameValue>
</Property>
</PropertyList>
</UserArea>
</root>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/321720.html
