XSLT设置默认值为' 0'如果节点不存在

时间:2016-09-13 20:07:04

标签: xml xslt

我正在尝试将三个节点添加到一起以定义一个总值,但我得到的输出是" NaN"当给定XML文档中不存在一个或多个所选节点时。定义' 0'的默认值的最简单方法是什么?对于节点' double',' triple'和' hr'只有在给定的XML中不存在特定节点时才会选择?

以下是定义了所有节点的输入XML示例:

<totals> 
  <hitting ab="32" r="9" h="9" rbi="9" double="3" triple="1" hr="1" bb="5" hbp="1" sh="1" so="4" gdp="1" ground="13" fly="6" kl="2" hitdp="1"></hitting>  
  <fielding po="27" a="13" e="1" csb="0" sba="2"></fielding>  
  <hsitsummary adv="3" fly="6" ground="13" lob="6" rcherr="1" rchfc="1" rbi-2out="6" vsleft="0,0" advops="13,23" leadoff="4,8" pinchhit="0,0" w2outs="3,10" wloaded="1,1" wrbiops="5,16" wrunners="6,19"></hsitsummary>  
  <pitching ip="9.0" ab="31" bb="1" bf="32" double="1" er="0" fly="5" ground="9" h="4" kl="2" r="0" so="12" sho="1"></pitching>  
  <psitsummary fly="5" ground="9" leadoff="3,9" wrunners="1,10" vsleft="2,15" w2outs="1,10"></psitsummary> 
</totals>  

当只定义了一个(&#39; double&#39;)时:

<totals> 
  <hitting ab="31" r="0" h="4" rbi="0" double="1" bb="1" sb="2" cs="1" so="12" ground="9" fly="5" kl="2"></hitting>  
  <fielding po="24" a="11" e="1" indp="1"></fielding>  
  <hsitsummary fly="5" ground="9" lob="5" rcherr="1" vsleft="0,0" advops="1,10" leadoff="3,9" pinchhit="0,2" w2outs="1,10" wloaded="0,0" wrbiops="0,7" wrunners="1,10"></hsitsummary>  
</totals>

到目前为止我尝试过的XSLT转换,显然只有在定义了所有三个元素时才有效。否则它会吐出“NaN&#39;。

<xbh><xsl:value-of select="floor(totals/hitting/@double + totals/hitting/@triple + totals/hitting/@hr)" /></xbh>

3 个答案:

答案 0 :(得分:1)

而不是totals/hitting/@double + totals/hitting/@triple + totals/hitting/@hr使用sum(totals/hitting/@double | totals/hitting/@triple | totals/hitting/@hr)

答案 1 :(得分:0)

您可以使用LogManager.Configuration

进行管理
<xsl:choose>

答案 2 :(得分:0)

XSLT 2.0

<xbh>
    <xsl:value-of select="totals/hitting/sum(@double | @triple | @hr)"/>
</xbh>