Enrich Mediator仅适用于WSO2 ESB 5中的First元素

时间:2017-02-27 15:03:01

标签: xml xpath wso2 wso2esb

我正在使用Enrich Mediator来丰富XML。

我的问题是,它始终只适用于第一个元素,尽管我想将它应用于多个元素。如何将它应用于XPATH选择的所有元素?

我也试过以下选项。但是失败了。

//Response/ResponseDetails/SearchHotelPriceResponse/HotelDetails/Hotel[@HasExtraInfo="true"]

丰富配置:

<enrich>
    <source type="inline">
        <ImageCode xmlns="">IMG10004</ImageCode>
    </source>
    <target action="child" xpath="//Response/ResponseDetails/SearchHotelPriceResponse/HotelDetails/Hotel[*]"/>
</enrich>

XML Payload:

<Response ResponseReference="REF_D_028_749-2801486459143247">
    <ResponseDetails Language="en">
        <SearchHotelPriceResponse>
            <HotelDetails>
                <Hotel HasExtraInfo="true" HasMap="true" HasPictures="true">
                    <City Code="LON">London</City>
                    <Item Code="ALE1">ALEXANDRA</Item>
                    <StarRating>3</StarRating>
                    <HotelRooms>
                        <HotelRoom Code="SB" NumberOfRooms="1"/>
                    </HotelRooms>
                </Hotel>
                <Hotel HasExtraInfo="true" HasPictures="true">
                    <City Code="LON">London</City>
                    <Item Code="ALO">Aloft London Excel</Item>
                    <StarRating>4</StarRating>
                    <HotelRooms>
                        <HotelRoom Code="SB" NumberOfRooms="1"/>
                    </HotelRooms>
                </Hotel>
                <Hotel HasExtraInfo="true" HasMap="true" HasPictures="true">
                    <City Code="LON">London</City>
                    <Item Code="AMB3">Ambassadors Bloomsbury</Item>
                    <StarRating>4</StarRating>
                    <HotelRooms>
                        <HotelRoom Code="SB" NumberOfRooms="1"/>
                    </HotelRooms>
                </Hotel>
            </HotelDetails>
        </SearchHotelPriceResponse>
    </ResponseDetails>
</Response>
  • ESB版本5.0.0

2 个答案:

答案 0 :(得分:5)

使用&#34;迭代&#34; mediator with aggregate for proccess所有元素。请检查我的解决方案。

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="EnrichProxy"
       startOnLoad="true"
       statistics="disable"
       trace="disable"
       transports="http,https">
   <target>
      <inSequence>
         <payloadFactory media-type="xml">
            <format>
               <Response xmlns="" ResponseReference="REF_D_028_749-2801486459143247">
                  <ResponseDetails Language="en">
                     <SearchHotelPriceResponse>
                        <HotelDetails>
                           <Hotel HasExtraInfo="false" HasMap="true" HasPictures="true">
                              <City Code="LON">London</City>
                              <Item Code="ALE1">ALEXANDRA</Item>
                              <StarRating>3</StarRating>
                              <HotelRooms>
                                 <HotelRoom Code="SB" NumberOfRooms="1"/>
                              </HotelRooms>
                           </Hotel>
                           <Hotel HasExtraInfo="true" HasPictures="true">
                              <City Code="LON">London</City>
                              <Item Code="ALO">Aloft London Excel</Item>
                              <StarRating>4</StarRating>
                              <HotelRooms>
                                 <HotelRoom Code="SB" NumberOfRooms="1"/>
                              </HotelRooms>
                           </Hotel>
                           <Hotel HasExtraInfo="true" HasMap="true" HasPictures="true">
                              <City Code="LON">London</City>
                              <Item Code="AMB3">Ambassadors Bloomsbury</Item>
                              <StarRating>4</StarRating>
                              <HotelRooms>
                                 <HotelRoom Code="SB" NumberOfRooms="1"/>
                              </HotelRooms>
                           </Hotel>
                        </HotelDetails>
                     </SearchHotelPriceResponse>
                  </ResponseDetails>
               </Response>
            </format>
            <args/>
         </payloadFactory>
         <property expression="$body" name="bodyBackup" type="OM"/>
         <iterate expression="$body//Response/ResponseDetails/SearchHotelPriceResponse/HotelDetails/Hotel"
                  id="Hotels">
            <target>
               <sequence>
                  <filter regex="true()" source="$body//Hotel/@HasExtraInfo">
                     <then>
                        <enrich>
                           <source clone="true" type="inline">
                              <ImageCode xmlns="">IMG10004</ImageCode>
                           </source>
                           <target action="child" xpath="$body//Hotel"/>
                        </enrich>
                     </then>
                     <else/>
                  </filter>
                  <loopback/>
               </sequence>
            </target>
         </iterate>
      </inSequence>
      <outSequence>
         <property name="HotelDetails" scope="default">
            <HotelDetails xmlns=""/>
         </property>
         <aggregate id="Hotels">
            <completeCondition>
               <messageCount max="-1" min="-1"/>
            </completeCondition>
            <onComplete enclosingElementProperty="HotelDetails" expression="$body/*[1]">
               <enrich>
                  <source clone="true" xpath="$body/*[1]"/>
                  <target xpath="$ctx:bodyBackup//Response/ResponseDetails/SearchHotelPriceResponse/HotelDetails"/>
               </enrich>
               <enrich>
                  <source clone="true" xpath="$ctx:bodyBackup/*[1]"/>
                  <target type="body"/>
               </enrich>
               <send/>
            </onComplete>
         </aggregate>
      </outSequence>
   </target>
   <description/>
</proxy>

对于此解决方案,这是结果

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <Response ResponseReference="REF_D_028_749-2801486459143247">
         <ResponseDetails Language="en">
            <SearchHotelPriceResponse>
               <HotelDetails>
                  <Hotel HasExtraInfo="false" HasMap="true" HasPictures="true">
                     <City Code="LON">London</City>
                     <Item Code="ALE1">ALEXANDRA</Item>
                     <StarRating>3</StarRating>
                     <HotelRooms>
                        <HotelRoom Code="SB" NumberOfRooms="1"/>
                     </HotelRooms>
                  </Hotel>
                  <Hotel HasExtraInfo="true" HasPictures="true">
                     <City Code="LON">London</City>
                     <Item Code="ALO">Aloft London Excel</Item>
                     <StarRating>4</StarRating>
                     <HotelRooms>
                        <HotelRoom Code="SB" NumberOfRooms="1"/>
                     </HotelRooms>
                     <ImageCode>IMG10004</ImageCode>
                  </Hotel>
                  <Hotel HasExtraInfo="true" HasMap="true" HasPictures="true">
                     <City Code="LON">London</City>
                     <Item Code="AMB3">Ambassadors Bloomsbury</Item>
                     <StarRating>4</StarRating>
                     <HotelRooms>
                        <HotelRoom Code="SB" NumberOfRooms="1"/>
                     </HotelRooms>
                     <ImageCode>IMG10004</ImageCode>
                  </Hotel>
               </HotelDetails>
            </SearchHotelPriceResponse>
         </ResponseDetails>
      </Response>
   </soapenv:Body>
</soapenv:Envelope>

答案 1 :(得分:1)

使用&#34; foreach&#34;调解员迭代&#34;酒店&#34;节点及其内部序列,您可以使用rich mediator(将修改单个节点)