【问题标题】:Rename XMLnode in Mule flow在 Mule 流中重命名 XMLnode
【发布时间】:2018-03-14 04:14:45
【问题描述】:

我有一个带有 XML 有效负载的 Mule 流,例如:

<?xml version="1.0" encoding="utf-16"?>
<root type="1" name="blah">
  <blablah value="10" desc="Material" />
</root>

我想重命名“根”节点并尝试使用 xml-to-dom-transformer 和表达式组件。但是,我不知道该怎么做。我尝试了这样的方法,但没有帮助:

    <expression-component><![CDATA[
      node = message.payload.getRootElement();
      node.renameNode = 'peo';
    ]]></expression-component>

问候

【问题讨论】:

  • 你会如何在 java 或任何其他编程工具中做同样的事情?
  • 我会在 C# 中使用 LINQ2XML 或简单的 Python oneliner 以代码方式进行。虽然不会在骡子流中帮助我

标签: xml mule dom4j


【解决方案1】:

基本上,我建议采用与 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>

【讨论】:

    【解决方案2】:

    您可以在 Mule 中使用 XSLT 转换器来修改您的 XML
    参考:- https://docs.mulesoft.com/mule-user-guide/v/3.7/xslt-transformer
    在 Mule 社区版中,可以轻松地将 XML 的任何元素/节点、值、属性修改为您自己的自定义格式。
    例如下面的 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>
    

    这里根元素&lt;root&gt;改为&lt;peo&gt;

    【讨论】:

      【解决方案3】:

      您可以使用 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>
      

      【讨论】:

      • 谢谢,但这不是 Enterprise Mule,如果我没记错的话,Dataweave 在社区运行时不可用?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多