在 WSO2 ESB 中组合迭代/聚合器和 XSLT 调解器无法按预期工作



我正在使用调用数据服务并向其发送以下消息的代理服务:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:add="http://www.example.org/Address/">
   <soap:Body>
      <dat:getmpartybranch xmlns:dat="http://ws.wso2.org/dataservice">
         <dat:clientid>473906852857651</dat:clientid>
      </dat:getmpartybranch>
   </soap:Body>
</soap:Envelope>

并收到此响应:

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
   <soapenv:Header></soapenv:Header>
   <soapenv:Body>
      <DataCollection xmlns="http://ws.wso2.org/dataservice">
         <Datalist>
            <partybranchid>796243010946586</partybranchid>
            <clientid>473906852857651</clientid>
         </Datalist>
         <Datalist>
            <partybranchid>2500000000</partybranchid>
            <clientid>473906852857651</clientid>
         </Datalist>
      </DataCollection>
   </soapenv:Body>
</soapenv:Envelope>

代理遍历每个 Datalist 节点,并使用客户端 id 和 partybranchid 的值创建另一个请求,发送到另一个数据服务:

请求 1:

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
   <soapenv:Body>
      <dat:getselect_addresses xmlns:dat="http://ws.wso2.org/dataservice">
         <dat:objectid>796243010946586</dat:objectid>
         <dat:clientid>473906852857651</dat:clientid>
      </dat:getselect_addresses>
   </soapenv:Body>
</soapenv:Envelope>

请求 2:

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
   <soapenv:Body>
      <dat:getselect_addresses xmlns:dat="http://ws.wso2.org/dataservice">
         <dat:objectid>2500000000</dat:objectid>
         <dat:clientid>473906852857651</dat:clientid>
      </dat:getselect_addresses>
   </soapenv:Body>
</soapenv:Envelope>

响应 1:

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
   <soapenv:Header></soapenv:Header>
   <soapenv:Body>
      <Addresses xmlns="http://ws.wso2.org/dataservice">
         <Address>
            <address>direccion1</address>
            <partybranchid>796243010946586</partybranchid>
            <clientid>473906852857651</clientid>
         </Address>
         <Address>
            <address>direccion3</address>
            <partybranchid>796243010946586</partybranchid>
            <clientid>473906852857651</clientid>
         </Address>
      </Addresses>
   </soapenv:Body>
</soapenv:Envelope>

响应 2:

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
   <soapenv:Header></soapenv:Header>
   <soapenv:Body>
      <Addresses xmlns="http://ws.wso2.org/dataservice">
         <Address>
            <address>yo</address>
            <partybranchid>2500000000</partybranchid>
            <clientid>473906852857651</clientid>
         </Address>
      </Addresses>
   </soapenv:Body>
</soapenv:Envelope>

在后续版本中,我使用了一个聚合器中介器,其中包含以下表达式//add:Addresses/add:Address,因此我的 out put 消息如下所示:

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
    <soapenv:Body>
        <Address xmlns="http://ws.wso2.org/dataservice"><address>direccion1</address><partybranchid>796243010946586</partybranchid><clientid>473906852857651</clientid></Address>
        <Address xmlns="http://ws.wso2.org/dataservice"><address>direccion3</address><partybranchid>796243010946586</partybranchid><clientid>473906852857651</clientid></Address>
        <Address xmlns="http://ws.wso2.org/dataservice"><address>yo</address><partybranchid>2500000000</partybranchid><clientid>473906852857651</clientid></Address>
    </soapenv:Body>
</soapenv:Envelope>

输出顺序:

  <outSequence>
     <aggregate id="iterate1">
        <completeCondition>
           <messageCount min="-1" max="-1"/>
        </completeCondition>
        <onComplete xmlns:s12="http://www.w3.org/2003/05/soap-envelope"
                    xmlns:add="http://ws.wso2.org/dataservice"
                    xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/"
                    expression="//add:Addresses/add:Address">
           <log level="full"/>
        </onComplete>
     </aggregate>
     <xslt key="conf:/xslt/Tranformacion_DS_to_ESB.xsl"/>
     <send/>
  </outSequence>

现在我想用XSLT转换这个消息,所以我使用ALTOVA XML间谍并构建这个"Tranformacion_DS_to_ESB.xsl":

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:a="http://ws.wso2.org/dataservice" xmlns:b="http://www.example.org/Address/" exclude-result-prefixes="a fn xs">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <b:getAddressResponse>
            <xsl:for-each select="//a:Address">
                    <address>
                        <address>
                            <xsl:value-of select="a:address"/>
                        </address>
                        <partybranchid>
                            <xsl:value-of select="a:partybranchid"/>
                        </partybranchid>
                        <clientid>
                            <xsl:value-of select="a:clientid"/>
                        </clientid>
                    </address>
            </xsl:for-each>
        </b:getAddressResponse>
    </xsl:template>
</xsl:stylesheet>

如果我使用上一条肥皂消息在 XML SPY 中测试此转换,结果是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<b:getAddressResponse xmlns:b="http://www.example.org/Address/">
    <address>
        <address>direccion1</address>
        <partybranchid>796243010946586</partybranchid>
        <clientid>473906852857651</clientid>
    </address>
    <address>
        <address>direccion3</address>
        <partybranchid>796243010946586</partybranchid>
        <clientid>473906852857651</clientid>
    </address>
    <address>
        <address>yo</address>
        <partybranchid>2500000000</partybranchid>
        <clientid>473906852857651</clientid>
    </address>
</b:getAddressResponse>

没关系,但是当我在聚合中介器完成工作后将此转换放入 ESB 中时,我收到以下错误消息:

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
   <soapenv:Body>
      <b:getAddressResponse xmlns:b="http://www.example.org/Address/">
         <address>
            <address>direccion1</address>
            <partybranchid>796243010946586</partybranchid>
            <clientid>473906852857651</clientid>
         </address>
      </b:getAddressResponse>
      <Address xmlns="http://ws.wso2.org/dataservice">
         <address>direccion3</address>
         <partybranchid>796243010946586</partybranchid>
         <clientid>473906852857651</clientid>
      </Address>
      <Address xmlns="http://ws.wso2.org/dataservice">
         <address>yo</address>
         <partybranchid>2500000000</partybranchid>
         <clientid>473906852857651</clientid>
      </Address>
   </soapenv:Body>
</soapenv:Envelope>

对这个结果有任何想法吗??

编辑:当我尝试 matthias 建议的代码时,我在 XML SPY 中得到了正确的输出,但在 ESB 中我得到了另一个错误的输出:

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
   <soapenv:Body>
      <address xmlns:b="http://www.example.org/Address/">
         <address>direccion1</address>
         <partybranchid>796243010946586</partybranchid>
         <clientid>473906852857651</clientid>
      </address>
      <Address xmlns="http://ws.wso2.org/dataservice">
         <address>direccion3</address>
         <partybranchid>796243010946586</partybranchid>
         <clientid>473906852857651</clientid>
      </Address>
      <Address xmlns="http://ws.wso2.org/dataservice">
         <address>yo</address>
         <partybranchid>2500000000</partybranchid>
         <clientid>473906852857651</clientid>
      </Address>
   </soapenv:Body>
</soapenv:Envelope>

最后更新:使用@Ratha建议我和这个链接 wso2 ESB:拆分/收集模式 - 单一响应 这是正确的配置。

这是我的结局:

  <outSequence>
     <property name="root" scope="default">
        <root:rootelement xmlns:root="http://ws.wso2.org/dataservice"/>
     </property>
     <aggregate id="iterate1">
        <completeCondition>
           <messageCount min="-1" max="-1"/>
        </completeCondition>
        <onComplete xmlns:s12="http://www.w3.org/2003/05/soap-envelope"
                    xmlns:add="http://ws.wso2.org/dataservice"
                    xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/"
                    expression="//add:Addresses/add:Address"
                    enclosingElementProperty="root">
           <log level="full"/>
        </onComplete>
     </aggregate>
     <xslt key="conf:/xslt/Tranformacion_DS_to_ESB.xsl"/>
     <send/>
  </outSequence>

如您所见,我将根元素作为聚合消息的根属性,并在onComplete配置中使用封闭元素属性="root",因此我的聚合输出现在如下所示:

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
    <soapenv:Body>
        <root:rootelement xmlns:root="http://ws.wso2.org/dataservice">
            <Address xmlns="http://ws.wso2.org/dataservice">
                <address>direccion1</address>
                <partybranchid>796243010946586</partybranchid>
                <clientid>473906852857651</clientid>
            </Address>
            <Address xmlns="http://ws.wso2.org/dataservice">
                <address>direccion3</address>
                <partybranchid>796243010946586</partybranchid>
                <clientid>473906852857651</clientid>
            </Address>
            <Address xmlns="http://ws.wso2.org/dataservice">
                <address>yo</address>
                <partybranchid>2500000000</partybranchid>
                <clientid>473906852857651</clientid>
            </Address>
        </root:rootelement>
    </soapenv:Body>
</soapenv:Envelope>

我更新了我的 XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:a="http://ws.wso2.org/dataservice" xmlns:b="http://www.example.org/Address/" exclude-result-prefixes="a fn xs">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <b:getAddressResponse>
            <xsl:for-each select="//a:rootelement/a:Address">
                    <address>
                        <address>
                            <xsl:value-of select="a:address"/>
                        </address>
                        <partybranchid>
                            <xsl:value-of select="a:partybranchid"/>
                        </partybranchid>
                        <clientid>
                            <xsl:value-of select="a:clientid"/>
                        </clientid>
                    </address>
            </xsl:for-each>
        </b:getAddressResponse>
    </xsl:template>
</xsl:stylesheet>

现在我的代理响应是这样的,正如我所愿:

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
   <soapenv:Body>
      <b:getAddressResponse xmlns:b="http://www.example.org/Address/">
         <address>
            <address>direccion1</address>
            <partybranchid>796243010946586</partybranchid>
            <clientid>473906852857651</clientid>
         </address>
         <address>
            <address>direccion3</address>
            <partybranchid>796243010946586</partybranchid>
            <clientid>473906852857651</clientid>
         </address>
         <address>
            <address>yo</address>
            <partybranchid>2500000000</partybranchid>
            <clientid>473906852857651</clientid>
         </address>
      </b:getAddressResponse>
   </soapenv:Body>
</soapenv:Envelope>

这里的问题是,您对 XSLT 调解器的请求没有根元素。也就是说,在皂体内,所有三个<Address>都处于同一水平。他们没有根。当 esb 将消息发送到 xslt 时,它将传递正文消息。在这里,xslt 将只选取第一个<Address>元素并进行转换。要克服,请用包裹根元素制作肥皂体。例如:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.samples" xmlns:xsd="http://services.samples/xsd">
   <soapenv:Header/>
   <soapenv:Body>
  <root xmlns="http://ws.wso2.org/dataservice">
     <Address xmlns="http://ws.wso2.org/dataservice">
        <address>direccion1</address>
        <partybranchid>796243010946586</partybranchid>
        <clientid>473906852857651</clientid>
     </Address>
        <Address xmlns="http://ws.wso2.org/dataservice">
            <address>direccion3</address>
            <partybranchid>796243010946586</partybranchid>
            <clientid>473906852857651</clientid>
        </Address>
        <Address xmlns="http://ws.wso2.org/dataservice">
            <address>yo</address>
            <partybranchid>2500000000</partybranchid>
            <clientid>473906852857651</clientid>
        </Address>
        </root>
   </soapenv:Body>
</soapenv:Envelope>

如果你像这样发送,你会得到这样的输出;

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsd="http://services.samples/xsd" xmlns:ser="http://services.samples">
    <soapenv:Body>
        <b:getAddressResponse xmlns:b="http://www.example.org/Address/">
            <address>
                <address></address>
                <partybranchid></partybranchid>
                <clientid></clientid>
            </address>
            <address>
                <address></address>
                <partybranchid></partybranchid>
                <clientid></clientid>
            </address>
            <address>
                <address></address>
                <partybranchid></partybranchid>
                <clientid></clientid>
            </address>
        </b:getAddressResponse>
    </soapenv:Body>
</soapenv:Envelope>

注意:使用正确的 xpath 编辑您的 xslt 。

最新更新