簡體   English   中英

需要幫助在WeatherWS應用程序返回的xml響應上編寫xpath表達式

[英]Need help writing xpath expression on xml response returned from WeatherWS app

我正在努力編寫一個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屬性值'true'。

我一直在使用以下在線工具 - http://www.freeformatter.com/xpath-tester.html來測試我的嘗試,但每次嘗試都返回了!沒有匹配。

這是xpath表達式我雖然是正確的:

//GetCityForecastByZIPResponse//GetCityForecastByZIPResult//Success

希望有人能提供幫助。

pS用於生成此響應的Web服務可以在這里找到: http ://wsf.cdyne.com/WeatherWS/Weather.asmx - 我正在使用GetCityForecastByZIP服務。 干杯!

這是由於命名空間所以你必須在你使用過的XPath引擎中預先“注冊”它們,或者只使用基於元素名稱的XPath,如下所示

//*[local-name()='GetCityForecastByZIPResponse']/*[local-name()='GetCityForecastByZIPResult']/*[local-name()='Success']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM