我正在尝试通过XPath从XML文件中提取节点,方法是在其子节点之一中查找特定值。
我在此位置有一个XML文件: Hourly forecasts for selected locations
这是其中的一部分:
SELECT pt.id,
pt.plan_name,
pt.product_disc_amt,
pt.product_disc_perc,
SUM(ac.premium_amount) AS premium,
MAX(ac.id) AS ac_id
FROM product pt
INNER JOIN age_classification ac
ON ac.p_id = pt.id
WHERE pt.medical_coverage_for = 'dependents_only'
OR (ac.relationship = 'child' AND ac.age_from::int <= 3 AND ac.age_to:: int >= 3
AND ac.gender = 'female' AND ac.marital_status = 'single')
OR (ac.relationship = 'child' AND ac.age_from::int <= 4 AND ac.age_to:: int >= 4
AND ac.gender = 'male' AND ac.marital_status = 'single' )
OR (ac.relationship = 'child' AND ac.age_from::int <= 2
AND ac.age_to:: int >= 2 AND ac.gender = 'female' AND ac.marital_status = 'single')
GROUP BY pt.id,
pt.plan_name,
ac.premium_amount,
pt.product_disc_perc
我想仅提取特定城市的完整天气预报。 城市名称存储在/ Location / LocationMetaData / LocationName下,而精算数据则存储在/ Location / LocationData下。
看了这个问题之后: XPath: Select first element with a specific attribute,我尝试使用此XPath字符串:
<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>
<LocationMetaData>
<LocationName>AFULA NIR HAEMEQ</LocationName>
<LocationLatitude>32.596</LocationLatitude>
<LocationLongitude>35.277</LocationLongitude>
<LocationHeight>60</LocationHeight>
</LocationMetaData>
<LocationData>
<Forecast>
<ForecastTime>10/12/2019 04:00 UTC</ForecastTime>
<Temperature>11.3</Temperature>
<RelativeHumidity>100</RelativeHumidity>
<WindSpeed>1.4</WindSpeed>
<WindDirection>34</WindDirection>
</Forecast>
<Forecast>
<ForecastTime>10/12/2019 05:00 UTC</ForecastTime>
<Temperature>11.7</Temperature>
<RelativeHumidity>100</RelativeHumidity>
<WindSpeed>1.5</WindSpeed>
<WindDirection>31</WindDirection>
</Forecast>
<Forecast>
<ForecastTime>10/12/2019 06:00 UTC</ForecastTime>
<Temperature>13.8</Temperature>
<RelativeHumidity>100</RelativeHumidity>
<WindSpeed>1.3</WindSpeed>
<WindDirection>27</WindDirection>
</Forecast>
</LocationData>
</Location>
<Location>
<LocationMetaData>
<LocationName>AVDAT</LocationName>
<LocationLatitude>30.788</LocationLatitude>
<LocationLongitude>34.771</LocationLongitude>
<LocationHeight>555</LocationHeight>
</LocationMetaData>
<LocationData>
<Forecast>
<ForecastTime>10/12/2019 04:00 UTC</ForecastTime>
<Temperature>10.5</Temperature>
<RelativeHumidity>95</RelativeHumidity>
<WindSpeed>1.2</WindSpeed>
<WindDirection>238</WindDirection>
</Forecast>
<Forecast>
<ForecastTime>10/12/2019 05:00 UTC</ForecastTime>
<Temperature>11.6</Temperature>
<RelativeHumidity>91</RelativeHumidity>
<WindSpeed>1.4</WindSpeed>
<WindDirection>251</WindDirection>
</Forecast>
<Forecast>
<ForecastTime>10/12/2019 06:00 UTC</ForecastTime>
<Temperature>13.9</Temperature>
<RelativeHumidity>83</RelativeHumidity>
<WindSpeed>1.5</WindSpeed>
<WindDirection>258</WindDirection>
</Forecast>
</LocationData>
</Location>
<Location>
<LocationMetaData>
<LocationName>BEER SHEVA</LocationName>
<LocationLatitude>31.251</LocationLatitude>
<LocationLongitude>34.799</LocationLongitude>
<LocationHeight>279</LocationHeight>
</LocationMetaData>
<LocationData>
<Forecast>
<ForecastTime>10/12/2019 04:00 UTC</ForecastTime>
<Temperature>12.2</Temperature>
<RelativeHumidity>98</RelativeHumidity>
<WindSpeed>0.7</WindSpeed>
<WindDirection>136</WindDirection>
</Forecast>
<Forecast>
<ForecastTime>10/12/2019 05:00 UTC</ForecastTime>
<Temperature>12.6</Temperature>
<RelativeHumidity>95</RelativeHumidity>
<WindSpeed>0.7</WindSpeed>
<WindDirection>145</WindDirection>
</Forecast>
</LocationData>
</Location>
</HourlyLocationsForecast>
我假设我可以与子节点中的值以及节点本身中的属性值相关。
我得到“不匹配”作为答案。
我在做什么错了?
答案 0 :(得分:1)
两个问题:
LocationMetaData
是 元素,不是属性,因此请删除前面的@
。JERUSALEM CENTRE
根本不在XML中显示,因此请过滤实际存在的LocationName
(例如AFULA NIR HAEMEQ
)。此更正后的XPath,
//HourlyLocationsForecast/Location[LocationMetaData/LocationName='AFULA NIR HAEMEQ']
实际上会根据您的XML选择一些内容。