可能由于命名空间的原因,无法使用 ForEach 调解器迭代来自 DSS 的结果

Cannot iterate through results from DSS with ForEach mediator possibly due to namespace

我在使用 XPath 查找 DSS 服务返回的结果中的任何数据时遇到了很大的困难。

这是返回数据的示例:

<?xml version='1.0' encoding='utf-8'?>
<Entries xmlns="http://ws.wso2.org/dataservice">
<Entry>
    <FirstName>Sandra</FirstName>
    <LastName>Carr</LastName>
    <FlightDate>2016-07-23T18:24:12.000-04:00</FlightDate>
    <Duration>2.8</Duration>
    <FlightEndTime>2016-07-23T21:24:12.000-04:00</FlightEndTime>
</Entry>
<Entry>
    <FirstName>Lawrence</FirstName>
    <LastName>Day</LastName>
    <FlightDate>2016-07-23T18:02:21.000-04:00</FlightDate>
    <Duration>2.8</Duration>
    <FlightEndTime>2016-07-23T21:02:21.000-04:00</FlightEndTime>
</Entry>

我有一个简单的突触 API 序列是

<?xml version="1.0" encoding="UTF-8"?>
<api context="/pilots" name="GetPilots" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="GET">
    <inSequence>
        <call>
            <endpoint key="GetPilotsRestEndpoint"/>
        </call>
        <foreach description="" expression="/Entries/Entry" id="field">
            <sequence>
                <log description="" level="custom">
                    <property name="tag" value="Entry"/>
                </log>
            </sequence>
        </foreach>
        <log level="full"/>
        <respond/>
    </inSequence>
    <outSequence/>
    <faultSequence/>
</resource>

我没有得到任何结果。我想知道是否是因为 "Entries" 标记中的命名空间。

无论如何,有人可以帮助我如何迭代结果吗?

谢谢。

是的,一定是命名空间问题。试试这个。

<foreach description="" expression="//ns:Entries/ns:Entry" id="field"
xmlns:ns="http://ws.wso2.org/dataservice">

这里有两件事。 1. 您的负载有一个命名空间 http://ws.wso2.org/dataservice,您需要将其添加到您的 XPath 2. 后端的响应如下所示。

<Entries xmlns="http://ws.wso2.org/dataservice">
<Entry>
    <FirstName>Sandra</FirstName>
    <LastName>Carr</LastName>
    <FlightDate>2016-07-23T18:24:12.000-04:00</FlightDate>
    <Duration>2.8</Duration>
    <FlightEndTime>2016-07-23T21:24:12.000-04:00</FlightEndTime>
</Entry>
<Entry>
    <FirstName>Lawrence</FirstName>
    <LastName>Day</LastName>
    <FlightDate>2016-07-23T18:02:21.000-04:00</FlightDate>
    <Duration>2.8</Duration>
    <FlightEndTime>2016-07-23T21:02:21.000-04:00</FlightEndTime>
</Entry>

但是,当它到达 ESB 时,它会被 SOAP 信封包裹起来,如下所示。您可以通过启用线路日志或在 call 调解器之后添加 <log level="full"/> 来验证这一点。

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <Entries xmlns="http://ws.wso2.org/dataservice">
            <Entry>
                <FirstName>Sandra</FirstName>
                <LastName>Carr</LastName>
                <FlightDate>2016-07-23T18:24:12.000-04:00</FlightDate>
                <Duration>2.8</Duration>
                <FlightEndTime>2016-07-23T21:24:12.000-04:00</FlightEndTime>
            </Entry>
            <Entry>
                <FirstName>Lawrence</FirstName>
                <LastName>Day</LastName>
                <FlightDate>2016-07-23T18:02:21.000-04:00</FlightDate>
                <Duration>2.8</Duration>
                <FlightEndTime>2016-07-23T21:02:21.000-04:00</FlightEndTime>
            </Entry>
        </Entries>
    </soapenv:Body>
</soapenv:Envelope>

因此,您的 XPath /Entries/Entry 将不起作用,因为起始元素不是 Entries 而是 Envelope。

请使用以下配置(使用 //)代替 Entries 元素出现在负载中的任何位置。

<foreach xmlns:ns="http://ws.wso2.org/dataservice" id="field" expression="//ns:Entries/ns:Entry">