Mule ESB Dataweave使用两个XML流,创建一个XML输出

时间:2017-03-24 14:11:31

标签: xml mule esb dataweave

enter image description here

我有两个来自Bigcommerce API的XML流。一个XML流包含订单信息,另一个流包含订单项(产品)。我试图将两个XML流与dataweave结合起来,并获得一个xml文档作为输出。当我按附件运行流程时,我没有得到所有产品,只有一组产品(第一组)

=============================================== ===================

这是order.xml流:

<?xml version="1.0"?>
<order>
<id>40144</id>
<date>2016-05-01</date>
</order>

以下是产品流(请注意,我们将在此处提供多种产品):

<?xml version="1.0"?>
<products>
<product>
<id>1234</id>
<order_id>40144</order_id>
<product_id>12345</product_id>
<order_address_id>12</order_address_id>
<name>Widget</name>
</product>
<product>
<id>53245</id>
<order_id>40144</order_id>
<product_id>56435</product_id>
<order_address_id>12</order_address_id>
<name>Super Widget</name>
</product>
</products>

这是Mule流配置(系统变量已更改)

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml"
xmlns:imaps="http://www.mulesoft.org/schema/mule/imaps"
xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" 
xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" 
xmlns:http="http://www.mulesoft.org/schema/mule/http" 
xmlns:imap="http://www.mulesoft.org/schema/mule/imap" 
xmlns="http://www.mulesoft.org/schema/mule/core" 
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/xml 
http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/imaps 
http://www.mulesoft.org/schema/mule/imaps/current/mule-imaps.xsd
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core 
http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http 
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/imap 
http://www.mulesoft.org/schema/mule/imap/current/mule-imap.xsd
http://www.mulesoft.org/schema/mule/objectstore 
http://www.mulesoft.org/schema/mule/objectstore/current/mule-objectstore.xsd
http://www.mulesoft.org/schema/mule/ee/dw 
http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
<imaps:connector name="IMAP" backupFolder="backup" validateConnections="true" backupEnabled="true" checkFrequency="900" doc:name="IMAP"/>
<http:request-config name="HTTP_Request_Configuration" protocol="HTTPS" host="myserver.net" port="443" doc:name="HTTP Request Configuration">
    <http:basic-authentication username="user" password="password"/>
</http:request-config>
<flow name="scattgathFlow">
    <imaps:inbound-endpoint host="192.168.1.1" port="993" responseTimeout="10000" user="user" password="password" doc:name="IMAP" connector-ref="IMAP"/> 
    <set-variable doc:name="Variable" value="#[message.inboundProperties.subject.replaceAll('[^0-9\\-]', '')]" variableName="email_subj"/>
    <http:request config-ref="HTTP_Request_Configuration" path="/api/v2/orders/#[email_subj]" method="GET" doc:name="HTTP" metadata:id="72233863-af08-4d0d-9a75-5b726b6d117e">
    </http:request> 
    <!--  Sub flow to retrieve products -->  
    <flow-ref name="payload1" doc:name="payload1" metadata:id="896da22a-11f4-4a80-9b38-85507261b002"/>
        <dw:transform-message metadata:id="8c4bb84a-74f8-4761-8f56-859dcf50473c" doc:name="Transform Message">
            <dw:input-payload doc:sample="products.xml"/>
            <dw:input-outbound-property propertyName="MULE_ENCODING"/>
            <dw:set-payload>
    <![CDATA[%dw 1.0
    %output application/xml
    %input payload application/xml
    ---
    {
    newxml: {
         billto: payload.order.customer_id as :string,
         shipping: payload.order.status,
    products: {
        productID: payload.products.product.product_id,
        sku: payload.products.product.sku,
        name: payload.products.product.name
    }
}
}]]></dw:set-payload>
</dw:transform-message>
<logger message="#[message.payload]" level="INFO" doc:name="Logger"/>
</flow>
<sub-flow name="payload1" >
    <http:request config-ref="HTTP_Request_Configuration" path="/api/v2/orders/#[email_subj]/products.xml" method="GET" doc:name="HTTP" metadata:id="4806e0ff-68f0-4295-baff-d65b07ae5190"/>
</sub-flow>
</mule>

1 个答案:

答案 0 :(得分:0)

你必须迭代payload.products.*product。这就是它的样子:

%dw 1.0
%output application/xml
%input payload application/xml
---
newxml: {
    products: {
        (payload.products.*product map {
            product: {
                productID: $.product_id,
                name: $.name
            }
        })
    }
}

从您的问题中使用<products>...</products>时,输出将如下所示:

<?xml version='1.0' encoding='UTF-8'?>
<newxml>
  <products>
    <product>
      <productID>12345</productID>
      <name>Widget</name>
    </product>
    <product>
      <productID>56435</productID>
      <name>Super Widget</name>
    </product>
  </products>
</newxml>

看看here,你会发现许多使用数据编辑进行转换的示例,包括使用map的示例。