英文:
How to write xml declaration using Mule DataWeave?
问题
需要在XML声明中添加standalone=yes,使用DataWeave。我尝试了不同的方法,但无法解决。
当前输出如下:
<?xml version='1.0' encoding='UTF-8'?>
<Input:xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="csm_admin.xsd">
<add>
<data>
<@firstName>jane</@firstName>
<lastName>ALDANA</lastName>
</data>
</add>
<add>
<data>
<@firstName>Emma</@firstName>
<lastName>EDWARDS</lastName>
</data>
</add>
</Input>
这是我期望的XML输出:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<admin xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="csm_admin.xsd">
<add>
<data>
<@firstName>jane</@firstName>
<lastName>ALDANA</lastName>
</data>
</add>
<add>
<data>
<@firstName>Emma</@firstName>
<lastName>EDWARDS</lastName>
</data>
</add>
</Input>
我尝试通过连接来添加standalone=yes声明,但也失败了。谢谢您的建议。
英文:
I need to do add standalone=yes to XML declaration using DataWeave. I have tried different approaches, but I can't get it resolved.
current output which I'm getting:
<?xml version='1.0' encoding='UTF-8'?>
<Input:xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="csm_admin.xsd">
<add>
<data>
<@firstName>jane</@firstName>
<lastName>ALDANA</lastName>
</data>
</add>
<add>
<data>
<@firstName>Emma</@firstName>
<lastName>EDWARDS</lastName>
</data>
</add>
</Input>
This is my Expected xml output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<admin xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="csm_admin.xsd">
<add>
<data>
<@firstName>jane</@firstName>
<lastName>ALDANA</lastName>
</data>
</add>
<add>
<data>
<@firstName>Emma</@firstName>
<lastName>EDWARDS</lastName>
</data>
</add>
</Input>
I tried to add standalone=yes declaration by concatenating but it is failing too.
Thanks for your input
答案1
得分: 2
以下是您要翻译的内容:
"Here's one approach you can try:
- Convert the XML payload to a string without including the existing declaration. Then, concatenate the formatted string with the new declaration. In DataWeave, set the output type to
text/plain
. - Use "Set Payload" to change the MIME type to
application/xml
in order to get the desired XML output.
Transform Message
%dw 2.0
var in = write(payload,"application/xml", writeDeclaration:false)
output text/plain
---
'<?xml version = "1.0" encoding="UTF-8" standalone="yes"?>' ++ in
Set Payload
<set-payload value="#[payload]" doc:name="Set Payload" doc:id="664c8d66-0bd7-4185-b754-1c4bc932221e" mimeType="application/xml"/>
Input
<?xml version='1.0' encoding='UTF-8'?>
<employees>
<employee>
<name>
<lastname>Kelly</lastname>
<firstname>Grace</firstname>
</name>
<hiredate>October 15, 2005</hiredate>
<projects>
<project>
<product>Printer</product>
</project>
</projects>
</employee>
</employees>
Output
<?xml version = "1.0" encoding="UTF-8" standalone="yes"?><employees>
<employee>
<name>
<lastname>Kelly</lastname>
<firstname>Grace</firstname>
</name>
<hiredate>October 15, 2005</hiredate>
<projects>
<project>
<product>Printer</product>
</project>
</projects>
</employee>
</employees>
"
英文:
Here's one approach you can try:
- Convert the XML payload to a string without including the existing declaration. Then, concatenate the formatted string with the new declaration. In DataWeave, set the output type to
text/plain
. - Use "Set Payload" to change the MIME type to
application/xml
in order to get the desired XML output.
Transform Message
%dw 2.0
var in = write(payload,"application/xml", writeDeclaration:false)
output text/plain
---
'<?xml version = "1.0" encoding="UTF-8" standalone="yes"?>' ++ in
Set Payload
<set-payload value="#[payload]" doc:name="Set Payload" doc:id="664c8d66-0bd7-4185-b754-1c4bc932221e" mimeType="application/xml"/>
Input
<?xml version='1.0' encoding='UTF-8'?>
<employees>
<employee>
<name>
<lastname>Kelly</lastname>
<firstname>Grace</firstname>
</name>
<hiredate>October 15, 2005</hiredate>
<projects>
<project>
<product>Printer</product>
</project>
</projects>
</employee>
</employees>
Output
<?xml version = "1.0" encoding="UTF-8" standalone="yes"?><employees>
<employee>
<name>
<lastname>Kelly</lastname>
<firstname>Grace</firstname>
</name>
<hiredate>October 15, 2005</hiredate>
<projects>
<project>
<product>Printer</product>
</project>
</projects>
</employee>
</employees>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论