XPath函数——数值型函数

    数值型函数主要用于处理数值的函数。数值型函数有:ceiling(),floor(),number(),round(),sum()等。

    1、ceiling()

    ceiling(number)函数用于返回大于或等于参数number的最小整数。

    简单示例:

    xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
    <product per="4">
       <quantity>18</quantity>
    </product>

     xslt:

  1. <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" version="1.0" encoding="gb2312" indent="yes"/>    
        <xsl:template match="/product">
           <xsl:copy>
                <xsl:copy-of select="@per"/>
                <xsl:copy-of select="quantity"/>
                <xsl:element name="unit">
                    <xsl:value-of select="ceiling(quantity div @per)" />          
                </xsl:element>       
           </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>

     结果:

  1. <?xml version="1.0" encoding="gb2312"?>
    <product per="4">
        <quantity>18</quantity>
        <unit>5</unit>
    </product>

 

    2、floor()

    floor(number)用来返回小于等于数值number的最大整数。

    简单示例:

    xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
    <products>
        <product>
            <name>pork</name>   
            <price>14.56</price> 
        </product>
        <product>
            <name>beef</name>   
            <price>18.35</price> 
        </product>
        <product>
            <name>chicken</name>   
            <price>9.0</price> 
       </product>
        <product>
            <name>egg</name>   
            <price>4.1</price> 
        </product>
        <product>
            <name>cabbage</name>   
            <price>0.88</price> 
        </product>
    </products>

     xslt:

  1. <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="text" version="1.0" encoding="gb2312" indent="yes"/>    
        <xsl:template match="/products">
           <xsl:for-each select="product">
                <xsl:choose>
                    <xsl:when test="price &lt; 1">
                           <xsl:value-of select="concat(name,' price is less than 1')"/>                
                    </xsl:when>
                    <xsl:when test="price=floor(price)">
                        <xsl:value-of select="concat(name,' price is just ',floor(price))"/>                
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="concat(name,' price is more than ',floor(price))"/>        
                    </xsl:otherwise>
                </xsl:choose>
                <xsl:text>&#x000A;</xsl:text>       
           </xsl:for-each>
        </xsl:template>
    </xsl:stylesheet>

     结果:

  1. pork price is more than 14
    beef price is more than 18
    chicken price is just 9
    egg price is more than 4
    cabbage price is less than 1

 

    3、number()

    number(xpathExpression)函数用于将参数xpathExpression的值转换为数值。当number()函数没有参数是,上下文的节点作为参数。当参数无法转换为数值型返回NaN;当参数为Boolean型时,true返回1,false返回0.

    简单示例:

    xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
    <root>
       <ele>string</ele>
       <ele>123</ele>
       <ele>true</ele>
    </root>

    xslt:

  1. <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="text" version="1.0" encoding="gb2312" indent="yes"/>    
        <xsl:template match="/root">
           <xsl:for-each select="ele">
                <xsl:value-of select="number()"/>
                <xsl:text>&#x000A;</xsl:text>       
           </xsl:for-each>
           <xsl:value-of select="number(true())"/>
           <xsl:text>&#x000A;</xsl:text>
           <xsl:value-of select="number(false())"/>     
        </xsl:template>
    </xsl:stylesheet>

    结果:

  1. NaN
    123
    NaN
    1
    0

 

    4、round()

    round(xpathExpression)函数用于将参数xpathExpression的值进行四舍五入去整。xpathExpression是必选项,为无法转换为数值时,返回NaN(在XMLSpy当转换不成功时会报错),当为boolean时,true返回1,false返回0.

    简单示例:

    xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
    <numbers>
        <number>41.8</number>
        <number>0.4</number>
        <number>-0.3</number>
        <number>-0.6</number>
        <number>0</number>
    </numbers>

     xslt:

  1. <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="text" version="1.0" encoding="gb2312" indent="yes"/>    
        <xsl:template match="/numbers">
           <xsl:for-each select="number">
                <xsl:value-of select="concat('round(',.,') is ',round(.)) "/>
                <xsl:text>&#x000A;</xsl:text>       
           </xsl:for-each>
           <xsl:value-of select="concat('round(true()) is ', round(true()))"/>
           <xsl:text>&#x000A;</xsl:text>
           <xsl:value-of select="concat('round(true()) is ', round(false()))"/>
        </xsl:template>
    </xsl:stylesheet>

    结果:

  1. round(41.8) is 42
    round(0.4) is 0
    round(-0.3) is -0
    round(-0.6) is -1
    round(0) is 0
    round(true()) is 1
    round(true()) is 0

 

    5、sum()

    sum(node-set)函数用于计算节点集合node-set的数值之和。

转载于:https://www.cnblogs.com/zhaozhan/archive/2010/01/18/1651000.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值