8

値が数値の場合は変数に要素の値を持たせたいのですが、そうでない場合は変数に value を持たせたいです0

言い換えれば、XSLT に次の単純な同等物はありますか?

var foobar = is_numeric(element value) ? element value : 0

または、これをどのように書きますか?

<xsl:variable name="foobar" select=" ? " />
4

3 に答える 3

15

XPath 1.0:

<xsl:variable name="foobar">
  <xsl:choose>
    <xsl:when test="number($value) = number($value)">
      <xsl:value-of select="$value"/>
    </xsl:when>
    <xsl:otherwise>0</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

この巧妙なnumber($value) = number($value)数値テストの参照: Dimitre Novatchev 's answer to "Xpath test if is number" question .

于 2013-10-02T15:27:07.447 に答える
9

castable asXPath 2.0 では、はい、" "を使用できます

<xsl:variable name="foobar" as="xs:double"
   select="if (x castable as xs:double) then x else 0" />
于 2013-10-02T14:42:53.207 に答える