开发者

XSLT: number format

开发者 https://www.devze.com 2023-02-07 13:34 出处:网络
In XSL is function like CONTAIN, that if i have number with simbol like \"123112\'+:\" then doesn\'t take it.开发者_Python百科

In XSL is function like CONTAIN, that if i have number with simbol like "123112'+:" then doesn't take it.开发者_Python百科

to be more precise:

<Number>111111</Number>
<Number>123123+</Number>
<Number>222222</Number>
<Number>222222+</Number>

answer:

111111
222222

I'm stuck with xslt 1.0 version


Another approach, exploiting number to boolean conversion.

<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>

    <xsl:template match="/*">
        <xsl:apply-templates select="Number[boolean(number()) or . = 0]"/>
    </xsl:template>

    <xsl:template match="Number">
        <xsl:value-of select="."/>
        <xsl:text>&#xA;</xsl:text>
    </xsl:template>

</xsl:stylesheet>

With input:

<Numbers>
    <Number>111111</Number>
    <Number>123123+</Number>
    <Number>222222</Number>
    <Number>222222+</Number>
</Numbers>

Correct result:

111111
222222

Quoting the spec:

The boolean function converts its argument to a boolean as follows: a number is true if and only if it is neither positive or negative zero nor NaN


Use the following XPath to select all the nodes that contains numbers. It will skip the ones with a plus sign in them.

Number[number(.)=number(.)]

Should work with XSLT 1.0


Yet another solution :)

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="Number[not(.*.+1)]"/>
</xsl:stylesheet>

when this transformation is applied on the following XML document:

<t>
    <Number>111111</Number>
    <Number>123123+</Number>
    <Number>222222</Number>
    <Number>222222+</Number>
</t>

the wanted, correct result is produced:

<t>
  <Number>111111</Number>
  <Number>222222</Number>
</t>

Explanation: All Number elements for wich the expression:

not(.*.+1)

is true() are filtered out by the simple template rule:

 <xsl:template match="Number[not(.*.+1)]"/>

This is possible only if the string value of the Number element cannot be converted to a number. In this case .*.+1 evaluates to NaN and boolean(NaN) is false() by definition.

If the string value of the Number element can be converted to a number $num, then the above expression is equivalent to:

not($num*$num+1)

and $num*$num+1 >= 1 for any number $num, so, boolean(.*.+1) in this case is always true().

0

精彩评论

暂无评论...
验证码 换一张
取 消