在 Mule 流中重命名 XMLnode
Rename XMLnode in Mule flow
我有一个带有 XML 有效载荷的 Mule 流,例如:
<?xml version="1.0" encoding="utf-16"?>
<root type="1" name="blah">
<blablah value="10" desc="Material" />
</root>
我想重命名 "root" 节点并尝试使用 xml-to-dom-transformer 和表达式组件。但是,我不知道该怎么做 that.I 尝试了类似这样的方法但没有帮助:
<expression-component><![CDATA[
node = message.payload.getRootElement();
node.renameNode = 'peo';
]]></expression-component>
此致
您可以使用 Dataweave(转换消息)。
尝试:
%dw 1.0
%output application/xml encoding="UTF-8"
---
{
brandNewRoot @(type: payload.root.@type, name: payload.root.@name): {
(payload)
}
}
您将收到以下回复:
<?xml version='1.0' encoding='UTF-8'?>
<brandNewRoot type="1" name="blah">
<blablah desc="Material" value="10"></blablah>
</brandNewRoot>
您可以在 Mule 中使用 XSLT 转换器来修改您的 XML
参考:- https://docs.mulesoft.com/mule-user-guide/v/3.7/xslt-transformer
在 Mule 社区版中,将 XML 的任何 elements/nodes、值、属性修改为您自己的自定义格式将很容易。
例如下面的 xslt 脚本将允许您修改 root 元素名称:-
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="/">
<peo>
<xsl:attribute name="type">
<xsl:value-of select="root/@type" />
</xsl:attribute>
<xsl:attribute name="name">
<xsl:value-of select="root/@name" />
</xsl:attribute>
<blablah>
<xsl:attribute name="value">
<xsl:value-of select="root/blablah/@value" />
</xsl:attribute>
<xsl:attribute name="desc">
<xsl:value-of select="root/blablah/@desc" />
</xsl:attribute>
</rootmodified>
</peo>
</xsl:template>
</xsl:stylesheet>
这里根元素<root>
改为<peo>
基本上,我建议使用与 Anirban 相同的方法。但是,更简单的 XSLT。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="/">
<newRoot>
<xsl:attribute name="type">
<xsl:value-of select="root/@type" />
</xsl:attribute>
<xsl:attribute name="name">
<xsl:value-of select="root/@name" />
</xsl:attribute>
<xsl:copy-of select="root/node()" />
</newRoot>
</xsl:template>
</xsl:stylesheet>
我有一个带有 XML 有效载荷的 Mule 流,例如:
<?xml version="1.0" encoding="utf-16"?>
<root type="1" name="blah">
<blablah value="10" desc="Material" />
</root>
我想重命名 "root" 节点并尝试使用 xml-to-dom-transformer 和表达式组件。但是,我不知道该怎么做 that.I 尝试了类似这样的方法但没有帮助:
<expression-component><![CDATA[
node = message.payload.getRootElement();
node.renameNode = 'peo';
]]></expression-component>
此致
您可以使用 Dataweave(转换消息)。
尝试:
%dw 1.0
%output application/xml encoding="UTF-8"
---
{
brandNewRoot @(type: payload.root.@type, name: payload.root.@name): {
(payload)
}
}
您将收到以下回复:
<?xml version='1.0' encoding='UTF-8'?>
<brandNewRoot type="1" name="blah">
<blablah desc="Material" value="10"></blablah>
</brandNewRoot>
您可以在 Mule 中使用 XSLT 转换器来修改您的 XML
参考:- https://docs.mulesoft.com/mule-user-guide/v/3.7/xslt-transformer
在 Mule 社区版中,将 XML 的任何 elements/nodes、值、属性修改为您自己的自定义格式将很容易。
例如下面的 xslt 脚本将允许您修改 root 元素名称:-
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="/">
<peo>
<xsl:attribute name="type">
<xsl:value-of select="root/@type" />
</xsl:attribute>
<xsl:attribute name="name">
<xsl:value-of select="root/@name" />
</xsl:attribute>
<blablah>
<xsl:attribute name="value">
<xsl:value-of select="root/blablah/@value" />
</xsl:attribute>
<xsl:attribute name="desc">
<xsl:value-of select="root/blablah/@desc" />
</xsl:attribute>
</rootmodified>
</peo>
</xsl:template>
</xsl:stylesheet>
这里根元素<root>
改为<peo>
基本上,我建议使用与 Anirban 相同的方法。但是,更简单的 XSLT。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="/">
<newRoot>
<xsl:attribute name="type">
<xsl:value-of select="root/@type" />
</xsl:attribute>
<xsl:attribute name="name">
<xsl:value-of select="root/@name" />
</xsl:attribute>
<xsl:copy-of select="root/node()" />
</newRoot>
</xsl:template>
</xsl:stylesheet>