需要帮助在WeatherWS应用程序

时间:2016-08-24 04:12:55

标签: xml xpath soap

我正在努力编写一个xpath表达式来提取从以下xml响应中返回的值。

    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetCityForecastByZIPResponse xmlns="http://ws.cdyne.com/WeatherWS/">
         <GetCityForecastByZIPResult>
            <Success>true</Success>
            <ResponseText>City Found</ResponseText>
            <State>MD</State>
            <City>Columbia</City>
            <WeatherStationCity>Baltimore</WeatherStationCity>
            <ForecastResult>
               <Forecast>
                  <Date>2011-10-08T00:00:00</Date>
                  <WeatherID>4</WeatherID>
                  <Desciption>Sunny</Desciption>
                  <Temperatures>
                     <MorningLow>48</MorningLow>
                     <DaytimeHigh>78</DaytimeHigh>
                  </Temperatures>
                  <ProbabilityOfPrecipiation>
                     <Nighttime>00</Nighttime>
                     <Daytime>00</Daytime>
                  </ProbabilityOfPrecipiation>
               </Forecast>
               <Forecast>
                  <Date>2011-10-09T00:00:00</Date>
                  <WeatherID>4</WeatherID>
                  <Desciption>Sunny</Desciption>
                  <Temperatures>
                     <MorningLow>50</MorningLow>
                     <DaytimeHigh>83</DaytimeHigh>
                  </Temperatures>
                  <ProbabilityOfPrecipiation>
                     <Nighttime>00</Nighttime>
                     <Daytime>00</Daytime>
                  </ProbabilityOfPrecipiation>
               </Forecast>
               <Forecast>
                  <Date>2011-10-10T00:00:00</Date>
                  <WeatherID>4</WeatherID>
                  <Desciption>Sunny</Desciption>
                  <Temperatures>
                     <MorningLow>53</MorningLow>
                     <DaytimeHigh>82</DaytimeHigh>
                  </Temperatures>
                  <ProbabilityOfPrecipiation>
                     <Nighttime>00</Nighttime>
                     <Daytime>00</Daytime>
                  </ProbabilityOfPrecipiation>
               </Forecast>
               <Forecast>
                  <Date>2011-10-11T00:00:00</Date>
                  <WeatherID>2</WeatherID>
                  <Desciption>Partly Cloudy</Desciption>
                  <Temperatures>
                     <MorningLow>57</MorningLow>
                     <DaytimeHigh>78</DaytimeHigh>
                  </Temperatures>
                  <ProbabilityOfPrecipiation>
                     <Nighttime>10</Nighttime>
                     <Daytime>10</Daytime>
                  </ProbabilityOfPrecipiation>
               </Forecast>
               <Forecast>
                  <Date>2011-10-12T00:00:00</Date>
                  <WeatherID>6</WeatherID>
                  <Desciption>Showers</Desciption>
                  <Temperatures>
                     <MorningLow>60</MorningLow>
                     <DaytimeHigh>71</DaytimeHigh>
                  </Temperatures>
                  <ProbabilityOfPrecipiation>
                     <Nighttime>50</Nighttime>
                     <Daytime>60</Daytime>
                  </ProbabilityOfPrecipiation>
               </Forecast>
               <Forecast>
                  <Date>2011-10-13T00:00:00</Date>
                  <WeatherID>6</WeatherID>
                  <Desciption>Showers</Desciption>
                  <Temperatures>
                     <MorningLow>61</MorningLow>
                     <DaytimeHigh>69</DaytimeHigh>
                  </Temperatures>
                  <ProbabilityOfPrecipiation>
                     <Nighttime>60</Nighttime>
                     <Daytime>50</Daytime>
                  </ProbabilityOfPrecipiation>
               </Forecast>
               <Forecast>
                  <Date>2011-10-14T00:00:00</Date>
                  <WeatherID>2</WeatherID>
                  <Desciption>Partly Cloudy</Desciption>
                  <Temperatures>
                     <MorningLow>59</MorningLow>
                     <DaytimeHigh>70</DaytimeHigh>
                  </Temperatures>
                  <ProbabilityOfPrecipiation>
                     <Nighttime>30</Nighttime>
                     <Daytime>40</Daytime>
                  </ProbabilityOfPrecipiation>
               </Forecast>
            </ForecastResult>
         </GetCityForecastByZIPResult>
      </GetCityForecastByZIPResponse>
   </soap:Body>
</soap:Envelope> 

有人可以编写xpath表达式来提取Success属性值&#39; true&#39;从这个回应。

我一直在使用以下在线工具 - http://www.freeformatter.com/xpath-tester.html来测试我的尝试,但每次尝试都返回了!没有匹配。

这是xpath表达式我虽然是正确的:

//GetCityForecastByZIPResponse//GetCityForecastByZIPResult//Success

希望有人可以提供帮助。

p.S用于生成此响应的Web服务可以在这里找到: http://wsf.cdyne.com/WeatherWS/Weather.asmx - 我正在使用GetCityForecastByZIP服务。 干杯!

1 个答案:

答案 0 :(得分:0)

这是因为命名空间,所以你要么必须注册&#39;它们位于您使用过的XPath引擎中,或者仅使用基于元素名称的XPath,如下所示

<div class="container" ng-controller="MessageBoardCtrl">
  <form>
    <input type="email" placeholder="Email Address" ng-model="email" required/>
    <textarea placeholder="Advertise your link here for Free..." rows="5" style="width:90%" ng-model="message" required=""></textarea>
    <button class="btn btn-primary" ng-click="sendMessage()">Post</button>
  </form>

  <p>{{item.message}}</p>

<script>
    function MessageBoardCtrl($scope, $http, $timeout) {
        $scope.items = [];
        $scope.message = '';
        $scope.email = '';
        $scope.lastTime = 0;

        $scope.refreshMessages = function() {
            $http.get('../templates/faucet.php/messages?time=' + $scope.lastTime).success(function(data) {
                for(id in data) {
                    item = data[id];
                    $scope.items.unshift(item);
                    if($scope.lastTime<item.time)
                        $scope.lastTime = item.time;
                }
            });
        }

        $scope.sendMessage = function() {
            if(!$scope.message)
                return;
            $http.post('../templates/faucet.php/add_message', {message: $scope.message, email: $scope.email}).success(function() {
                $scope.message = '';
                $scope.refreshMessages();
            });
        }

        $scope.periodicRefresh = function() {
            $scope.refreshMessages();
            $timeout($scope.periodicRefresh, 5000, true);
        }

        $scope.periodicRefresh();
    }
   </script>