我想在mule中的xml有效负载中添加一个节点。有人可以告诉我如何做到这一点。 输入xml -
<Location>
<cde> Hello </cde>
</Location>
我想在
之后添加一个节点结果xml shud就像这样 -
<Location>
<id> 1234 </id>
<cde> Hello </cde>
</Location>
我试过
<expression-component><![CDATA[
myNode = message.payload.rootElement.addElement(’ID’);
myNode.text = '1234';
message.payload.rootElement.elements().add(1, myNode.detach());
]]></expression-component>
也
<enricher source="#[sessionVars.providerid]" doc:name="Message Enricher"
target="#[xpath3(’/Locations’,payload,’NODE’).appendChild(payload.importNode($.getFirstChild(),true) )]">
<http:request config-ref="HTTP_Request_Configuration" path="/system" method="POST" doc:name="HTTP"/>
</enricher>
没有任何工作......请帮助!!!
答案 0 :(得分:0)
对于表达式组件解决方案,请尝试使用xml-to-dom-transformer
,然后在组件之后使用相反的变换器。
另一个干净的解决方案是使用XSLT identity transformation(添加id)。
最后,如果你使用Mule EE&lt; 3.7这对于DataMapper来说是一个非常简单的任务,如果在3.7上,则对DaveWeave更好。
答案 1 :(得分:0)
您可以使用 XSLT 添加节点或以下列方式修改XML: -
<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8082" basePath="rc" doc:name="HTTP Listener Configuration"/>
<flow name="test">
<http:listener config-ref="HTTP_Listener_Configuration" path="/myservice" doc:name="HTTP"/>
<set-variable variableName="Cde" value="#[xpath3('//Location/cde')]" doc:name="Variable"/>
<logger level="INFO" message="Cde :-#[flowVars.Cde]" doc:name="Logger"/>
<set-variable variableName="Id" value="1324" doc:name="Variable"/>
<mulexml:xslt-transformer name="PrepareSOAPRequest" xsl-file="Transform.xsl" outputEncoding="UTF-8" encoding="UTF-8" maxIdleTransformers="2" maxActiveTransformers="5" returnClass="java.lang.String" doc:name="XSLT">
<mulexml:context-property key="Cde" value="#[flowVars.Cde]"/> <!-- Passing the variables in XSLT to produce the XML dynamically -->
<mulexml:context-property key="Id" value="#[flowVars.Id]"/> <!-- Passing the variables in XSLT to produce the XML dynamically -->
</mulexml:xslt-transformer>
<logger level="INFO" message="Final XML :-#[message.payload]" doc:name="Logger"/>
</flow>
您的 XSLT 将如下所示,需要放在src/main/resource
文件夹下: -
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<!-- Getting values from Mule variables -->
<xsl:param name="Cde"/>
<xsl:param name="Id"/>
<xsl:template match="/">
<Location>
<id><xsl:value-of select="$Id"/></id>
<cde><xsl:value-of select="$Cde"/></cde>
</Location>
</xsl:template>
</xsl:stylesheet>
如您所见,您需要先使用 XPATH3 从输入XML中提取 cde 的值并将其存储到变量中。您还可以将 id 值存储到变量..
最后,您可以使用给定的 XSLT 修改XML,并将所有变量值传递给它,如上所示。
更新的答案
<enricher source="#[message.payload]" target="#[flowVars.test]">
<processor-chain>
<set-payload value="<Location><cde> Hello </cde></Location>" doc:name="Set Payload" />
<set-variable variableName="Cde" value="#[xpath3('//Location/cde')]" doc:name="Variable" />
<logger level="INFO" message="Cde :-#[flowVars.Cde]" doc:name="Logger" />
<set-variable variableName="Id" value="1324" doc:name="Variable" />
<mulexml:xslt-transformer name="PrepareSOAPRequest" xsl-file="Transform.xsl" outputEncoding="UTF-8" encoding="UTF-8" maxIdleTransformers="2" maxActiveTransformers="5" returnClass="java.lang.String"
doc:name="XSLT">
<mulexml:context-property key="Cde" value="#[flowVars.Cde]" /> <!-- Passing the variables in XSLT to produce the XML dynamically -->
<mulexml:context-property key="Id" value="#[flowVars.Id]" /> <!-- Passing the variables in XSLT to produce the XML dynamically -->
</mulexml:xslt-transformer>
<logger level="INFO" message="Final XML :-#[message.payload]" doc:name="Logger" />
<http:request config-ref="HTTP_Request_Configuration" path="/system" method="POST" doc:name="HTTP"/>
</processor-chain>
</enricher>
答案 2 :(得分:0)
我能够通过使用groovy
来做到这一点<enricher target="#[flowVars['compliant']]" doc:name="Message Enricher">
<flow-ref name="lookupComplaintData" doc:name="lookupComplaintData"/>
</enricher>
<set-payload value="#[flowVars.compliant]" doc:name="Set Payload"/>
<sub-flow doc:description="add Compliant information "
name="lookupComplaintData">
<scripting:transformer doc:name="Groovy">
<scripting:script engine="Groovy"><![CDATA[import org.dom4j.*
import groovy.xml.DOMBuilder
import groovy.xml.dom.DOMCategory
Element compliantElement = payload.getRootElement().addElement("compliant")
compliantElement.addElement("compliantType").addText("Delivery")
compliantElement.addElement("compliantReference").addText("ABC123ABC")
compliantElement.addElement("complaintText").addText("Order was delivered on time")
return payload
]]></scripting:script>
</scripting:transformer>