【问题标题】:Mule ESB Dataweave consume two XML streams, create one XML outputMule ESB Dataweave 使用两个 XML 流,创建一个 XML 输出
【发布时间】:2017-03-24 23:11:07
【问题描述】:

我有两个来自 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>

【问题讨论】:

  • 对您的问题的一些评论...它不包含实际问题,并且您的示例与数据编织中的有效负载不匹配。改进您的问题将有助于其他人为您的问题制定解决方案。

标签: xml mule esb dataweave


【解决方案1】:

你必须遍历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
            }
        })
    }
}

在您的问题中使用&lt;products&gt;...&lt;/products&gt; 时,输出将如下所示:

<?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,您会发现许多使用dataweave 进行转换的示例,包括使用map 的示例。

【讨论】:

    猜你喜欢
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多