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的数值之和。

posted @ 2010-01-18 20:45  Asharp  阅读(1757)  评论(0编辑  收藏  举报