简体   繁体   English

xPath-节点不存在时返回默认值?

[英]xPath - return default value when node absent ?

I saw multiple stackoverflow posts to find answer but could not. 我看到多个stackoverflow帖子以找到答案,但找不到。 Tried so many possiblities, but nothing seems to work. 尝试了很多可能性,但似乎没有任何效果。

I want to find gsRating of schools below, but many its optional tag. 我想在下面找到gsRating的学校,但是有很多可选标签。 So wanted to return -1 as default value when absent. 因此想在不存在时返回-1作为默认值。

Here is XML: 这是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&amp;id=87&amp;s_cid=gsapi</ratingsLink>
      <reviewsLink>http://www.greatschools.org/school/parentReviews.page?state=CA&amp;id=87&amp;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&amp;id=107&amp;s_cid=gsapi</ratingsLink>
      <reviewsLink>http://www.greatschools.org/school/parentReviews.page?state=CA&amp;id=107&amp;s_cid=gsapi</reviewsLink>
      <distance>1.14</distance>
      <schoolStatsLink>http://www.greatschools.org/modperl/achievement/CA/107</schoolStatsLink>
   </school>
</schools>

Tried as below: 尝试如下:

//schools/school[*]/(gsRating/string(),'-1')[1]

This XPath 2.0 expression, 这个XPath 2.0表达式,

for $s in //school 
   return concat($s/name, ', ', if ($s/gsRating) then $s/gsRating else '-1')

will return 将返回

James Leitch Elementary School, 10
Fred E. Weibel Elementary School, -1

for your sample XML. 用于您的示例XML。

Tried as below: 尝试如下:

 //schools/school[*]/(gsRating/string(),'-1')[1] 

Use this XPath 2.0 expression : 使用此XPath 2.0表达式

/*/*/(string(gsRating)[.], -1)[1]

If you want to discard gsRatings elements, whose string value is white-space , use: 如果要舍弃字符串值为空白的gsRatings元素 ,请使用:

/*/*/(normalize-space(gsRating)[.], -1)[1]

If you want to discard any gsRatings whose string value cannot be casted to number , use: 如果要丢弃字符串值不能转换为number的gsRatings ,请使用:

/*/*/(normalize-space(gsRating)[number(.) eq number(.)], -1)[1]

Here is an XSLT-based verification : 这是基于XSLT的验证

For simplicity, let's have this XML document: 为简单起见,让我们准备以下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>

When this XSLT transformation : 当此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>

is applied on the XML document above, the XPath expression is evaluated and the result it produces (a sequence of integers) is output: 在上面的XML文档上应用,评估XPath表达式并输出它产生的结果(整数序列):

10 -1 -1 -1 -1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM