我看到多个stackoverflow帖子找到答案,但不能。 尝试了很多可能性,但似乎没有任何效果。
我想在下面找到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]
答案 0 :(得分:1)
此XPath 2.0表达式,
for $s in //school
return concat($s/name, ', ', if ($s/gsRating) then $s/gsRating else '-1')
将返回
James Leitch Elementary School, 10
Fred E. Weibel Elementary School, -1
为您的示例XML。
答案 1 :(得分:1)
尝试如下:
//schools/school[*]/(gsRating/string(),'-1')[1]
使用此XPath 2.0表达式:
/*/*/(string(gsRating)[.], -1)[1]
如果要丢弃其字符串值为空格的gsRatings
元素,请使用:
/*/*/(normalize-space(gsRating)[.], -1)[1]
如果您要放弃其字符串值无法转换为数字的任何gsRatings
,请使用:
/*/*/(normalize-space(gsRating)[number(.) eq number(.)], -1)[1]
以下是基于XSLT的验证:
为简单起见,我们有这个XML文档:
<schools>
<school>
<gsRating>10</gsRating>
</school>
<school>
<noRating>10</noRating>
</school>
<school>
<gsRating> t </gsRating>
</school>
<school>
<gsRating/>
</school>
<school>
<gsRating> </gsRating>
</school>
</schools>
进行此XSLT转换时:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/*">
<xsl:value-of select=
"/*/*/(normalize-space(gsRating)[number(.) eq number(.)], -1)[1]"/>
</xsl:template>
</xsl:stylesheet>
应用于上面的XML文档,评估XPath表达式并输出它产生的结果(整数序列):
10 -1 -1 -1 -1