【问题标题】:WSO2 ESB foreach functionWSO2 ESB foreach 函数
【发布时间】:2016-07-14 14:54:16
【问题描述】:

在 WSO2 ESB 代理服务中,我如何根据来自某些 web 服务响应的整数值进行迭代,就像“foreach”一样:

例如这样的响应消息:

<Response>
   <noOfcustomers>10</noOfCustomers>
</Response>

我需要迭代 10 次(根据客户数量)

这可能吗?我怎样才能做到这一点?

感谢您的帮助!

【问题讨论】:

    标签: loops wso2 esb wso2esb


    【解决方案1】:

    我还没有找到一个干净的方法来做到这一点,但这是一个混乱的解决方案。

    首先您需要一个 XSLT 转换。

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="xsl xsi">
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
        <xsl:param name="iterations"/>
        <xsl:template name="for.loop">
            <xsl:param name="i"/>
            <xsl:param name="count"/>
            <!--begin_: Line_by_Line_Output -->
            <xsl:if test="$i &lt;= $count">
                <iteration>
                    <xsl:value-of select="$i"/>
                </iteration>
            </xsl:if>
            <!--begin_: RepeatTheLoopUntilFinished-->
            <xsl:if test="$i &lt;= $count">
                <xsl:call-template name="for.loop">
                    <xsl:with-param name="i">
                        <xsl:value-of select="$i + 1"/>
                    </xsl:with-param>
                    <xsl:with-param name="count">
                        <xsl:value-of select="$count"/>
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:if>
        </xsl:template>
        <xsl:template match="/">
            <iterations>
                <xsl:call-template name="for.loop">
                    <xsl:with-param name="i">1</xsl:with-param>
                    <xsl:with-param name="count"><xsl:value-of select="$iterations"/></xsl:with-param>
                </xsl:call-template>
            </iterations>
        </xsl:template>
    </xsl:stylesheet>
    

    然后你像这样在你的序列中使用转换:

    <inSequence>
        <xslt key="conf:/repository/test/iterations.xslt">
            <property name="iterations" expression="//noOfcustomers"/>
        </xslt>
        <iterate expression="//iterations/iteration" sequential="true">
            <target>
              <sequence>
    
              </sequence>
            </target>
        </iterate>
    </inSequence>
    

    迭代中介器中的序列将为“迭代”中的每个元素运行。这种方法的缺点是您将使用迭代 XML 替换消息正文,因此如果您希望重用原始消息,则必须在转换之前使用丰富的中介将原始消息保存到属性中。

    【讨论】:

    • 谢谢克里斯!!让我试试这个解决方案!
    • 这实际上是非常聪明的做法。
    【解决方案2】:

    你可以iterate基于xpath。但目前我们没有柜台支持。你的实际用例是什么?

    【讨论】:

    • 感谢您的回复。我需要发送一个带有确定费用(例如 10 个月)的信用请求,然后如果信用请求被批准用于外部 Web 服务,我需要发送 10 条消息来创建每个费用信息:付款日期、最低付款、付款截止日期等。
    • 您需要使用克隆中介吗? docs.wso2.org/wiki/display/ESB451/Clone+Mediator
    【解决方案3】:

    从 ESB 4.9 开始,ForEach 中介支持此功能

    【讨论】:

    • 我对此表示怀疑。 ForEach 还需要一个 xpath 表达式来操作。
    • 是的,没有办法遍历有效载荷中的计数器。但是有 FOREACH_COUNTER 可以帮助跟踪正在发生的迭代次数。
    猜你喜欢
    • 1970-01-01
    • 2017-10-19
    • 2014-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多