鞋子掉在润滑油里清洗:XSLT: Sum of products from multiple nodes

来源:百度文库 编辑:九乡新闻网 时间:2024/04/28 00:23:00

The XSL method sum(/foo) sums the value of all foo nodes in current context. If you, however, want to sum the product between two or more nodes the sum(/foo * /bar) is not sufficient as the product does not return a nodeset and thus sum() fails.

I found one way to sum products of multiple nodes and is to construct a temporary variable with the products, convert that variable to a nodeset and the sum all nodes in that temporary node set.

The function that converts a variable to a nodeset seems to be XSLT specific and the solution below is for Xalan since we are using it in our app. If you are using MSXML you will have to change the namespace but the name of the function is the same.

XML:

    
        
            2
        

        
            25
        

    

    
        
            10
        

        
            2
        

    

    
        
            23
        

        
            6
        

    

XSL:

    xmlns:xalan="http://xml.apache.org/xalan">