【发布时间】:2019-12-11 01:36:45
【问题描述】:
我正在尝试编写一个 Xpath 来从 XML 文件中检索部分数据,但只获得一级文本值,而不是我需要的整个节点树。
我正在使用来自该地址的动态 XML 文件 -
https://ims.data.gov.il/sites/default/files/IMS_001.xml
包含 10 个地点的天气预报。
部分数据示例如下所示:
<HourlyLocationsForecast>
<Identification>
<Organization>Israel Meteorological Service</Organization>
<Title>Hourly forecasts for selected locations</Title>
<IssueDateTime>Tue Dec 10 06:30:40 IST 2019</IssueDateTime>
</Identification>
<Location>...</Location>
<Location>...</Location>
<Location>...</Location>
<Location>...</Location>
<Location>...</Location>
<Location>...</Location>
<Location>...</Location>
<Location>
<LocationMetaData>
<LocationName>JERUSALEM CENTRE</LocationName>
<LocationLatitude>31.781</LocationLatitude>
<LocationLongitude>35.222</LocationLongitude>
<LocationHeight>810</LocationHeight>
</LocationMetaData>
<LocationData>
<Forecast>
<ForecastTime>10/12/2019 04:00 UTC</ForecastTime>
<Temperature>10.8</Temperature>
<RelativeHumidity>92</RelativeHumidity>
<WindSpeed>2.2</WindSpeed>
<WindDirection>223</WindDirection>
</Forecast>
<Forecast>
<ForecastTime>10/12/2019 05:00 UTC</ForecastTime>
<Temperature>11.0</Temperature>
<RelativeHumidity>91</RelativeHumidity>
<WindSpeed>2.2</WindSpeed>
<WindDirection>226</WindDirection>
</Forecast>
<Forecast>
<ForecastTime>10/12/2019 06:00 UTC</ForecastTime>
<Temperature>11.1</Temperature>
<RelativeHumidity>90</RelativeHumidity>
<WindSpeed>2.2</WindSpeed>
<WindDirection>229</WindDirection>
</Forecast>
</LocationData>
</Location>
<Location>...</Location>
</HourlyLocationsForecast>
我想提取 1 个位置的数据,即序列中的第 8 个。 所以,我试着写了这个 Xpath:
/HourlyLocationsForecast/Location[8]/*
我正在尝试获取该位置的所有数据。相反,我得到的只是位置参数,没有低于其水平的预测。
我做错了什么?
【问题讨论】:
-
您尚未展示如何应用 XPath 或如何使用结果,仍然难以回答或诊断您的问题。但是,请尝试仅选择带有 XPath
/HourlyLocationsForecast/Location[8]的Location元素并将其分配给一个变量,然后您可以进一步询问该元素以获取LocationMetadata或LocationData/Forecast信息。 -
@MadsHansen 非常感谢! You
re right I didnt 提到了我m implementing this. Im 使用软件工具(Automate BPA Server)的方式,而我m just interested in the proper way to write the XPath. The difference you told me (omitting that last/`所以不需要为*)做了很大的不同,所以现在我要去操纵我拥有的信息量更大的价值。