我看到多個stackoverflow帖子找到答案,但不能。 嘗試了很多可能性,但似乎沒有任何工作。xPath - 節點不存在時返回默認值?
我想找到gsRating下面的學校,但很多其可選標籤。所以想在沒有時返回-1作爲默認值。
下面是XML:
<?xml version="1.0" encoding="UTF-8"?>
<schools>
<school>
<gsId>87</gsId>
<name>James Leitch Elementary School</name>
<type>public</type>
<gradeRange>K-3</gradeRange>
<enrollment>1075</enrollment>
<gsRating>10</gsRating>
<parentRating>4</parentRating>
<city>Fremont</city>
<state>CA</state>
<districtId>8</districtId>
<district>Fremont Unified</district>
<districtNCESId>0614400</districtNCESId>
<address>47100 Fernald Street,
Fremont, CA 94539</address>
<phone>(510) 657-6100</phone>
<fax>(510) 659-9298</fax>
<website>http://www.jleitch.edu/</website>
<ncesId>061440001673</ncesId>
<lat>37.486492</lat>
<lon>-121.92332</lon>
<overviewLink>http://www.greatschools.org/california/fremont/87-James-Leitch-Elementary-School/?s_cid=gsapi</overviewLink>
<ratingsLink>http://www.greatschools.org/school/rating.page?state=CA&id=87&s_cid=gsapi</ratingsLink>
<reviewsLink>http://www.greatschools.org/school/parentReviews.page?state=CA&id=87&s_cid=gsapi</reviewsLink>
<distance>1.12</distance>
<schoolStatsLink>http://www.greatschools.org/modperl/achievement/CA/87</schoolStatsLink>
</school>
<school>
<gsId>107</gsId>
<name>Fred E. Weibel Elementary School</name>
<type>public</type>
<gradeRange>K-6</gradeRange>
<enrollment>853</enrollment>
<parentRating>4</parentRating>
<city>Fremont</city>
<state>CA</state>
<districtId>8</districtId>
<district>Fremont Unified</district>
<districtNCESId>0614400</districtNCESId>
<address>45135 South Grimmer Boulevard,
Fremont, CA 94539</address>
<phone>(510) 651-6958</phone>
<fax>(510) 651-6653</fax>
<website>http://www.fremont.k12.ca.us/weibel/</website>
<ncesId>061440009139</ncesId>
<lat>37.507336</lat>
<lon>-121.92837</lon>
<overviewLink>http://www.greatschools.org/california/fremont/107-Fred-E.-Weibel-Elementary-School/?s_cid=gsapi</overviewLink>
<ratingsLink>http://www.greatschools.org/school/rating.page?state=CA&id=107&s_cid=gsapi</ratingsLink>
<reviewsLink>http://www.greatschools.org/school/parentReviews.page?state=CA&id=107&s_cid=gsapi</reviewsLink>
<distance>1.14</distance>
<schoolStatsLink>http://www.greatschools.org/modperl/achievement/CA/107</schoolStatsLink>
</school>
</schools>
如下嘗試:
//schools/school[*]/(gsRating/string(),'-1')[1]