如何使用Mule DataWeave编写XML声明?

huangapple go评论50阅读模式
英文:

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:

  1. 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.
  2. 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:

  1. 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.
  2. 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,&quot;application/xml&quot;, writeDeclaration:false)
output text/plain
---
&#39;&lt;?xml version = &quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;&#39; ++ in

Set Payload

&lt;set-payload value=&quot;#[payload]&quot; doc:name=&quot;Set Payload&quot; doc:id=&quot;664c8d66-0bd7-4185-b754-1c4bc932221e&quot; mimeType=&quot;application/xml&quot;/&gt;

Input

&lt;?xml version=&#39;1.0&#39; encoding=&#39;UTF-8&#39;?&gt;
&lt;employees&gt;
  &lt;employee&gt;
    &lt;name&gt;
      &lt;lastname&gt;Kelly&lt;/lastname&gt;
      &lt;firstname&gt;Grace&lt;/firstname&gt;
    &lt;/name&gt;
    &lt;hiredate&gt;October 15, 2005&lt;/hiredate&gt;
    &lt;projects&gt;
      &lt;project&gt;
        &lt;product&gt;Printer&lt;/product&gt;
      &lt;/project&gt;
    &lt;/projects&gt;
  &lt;/employee&gt;
&lt;/employees&gt;

Output

&lt;?xml version = &quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;&lt;employees&gt;
  &lt;employee&gt;
    &lt;name&gt;
      &lt;lastname&gt;Kelly&lt;/lastname&gt;
      &lt;firstname&gt;Grace&lt;/firstname&gt;
    &lt;/name&gt;
    &lt;hiredate&gt;October 15, 2005&lt;/hiredate&gt;
    &lt;projects&gt;
      &lt;project&gt;
        &lt;product&gt;Printer&lt;/product&gt;
      &lt;/project&gt;
    &lt;/projects&gt;
  &lt;/employee&gt;
&lt;/employees&gt;

huangapple
  • 本文由 发表于 2023年7月18日 12:22:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709521.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定