XSL - sum multiplication of elements

前端 未结 2 1331
野性不改
野性不改 2021-01-14 00:40

I have a few items like this one:


    172
    42         


        
2条回答
  •  不要未来只要你来
    2021-01-14 01:24

    In addition to the correct answer by Kirill:

    I. XPath 2.0 (XSLT 2.0) solution:

    Use this single XPath 2.0 expression from any language hosting XPath 2.0:

    sum(/*/*/(quantity*unit-price))
    

    Here is a complete example using XSLT 2.0 as the host of XPath 2.0:

    
     
    
     
      
     
    
    

    when applied on the following XML document:

    
        
            1
            2
            7
        
        
            10
            20
            7
        
        
            100
            200
            7
        
    
    

    the wanted, correct result is produced:

    20202
    

    II. XSLT 1.0 solution using the transform-and-sum template/function of FXSL 1.x

    
       
    
       
    
       
    
       
    
        
          
            
            
          
        
    
        
          
          
        
    
    

    when this transformation is applied on the same XML document (above), the same correct result is produced:

    20202
    

    Do note: Using FXSL one doesn't need to implement explicit recursion "for the Nth time", the solution is compact and potential errors writing the recursion are eliminated altogether.

    As a whole the total development time, readability and flexibility are dramatically improved.

提交回复
热议问题