Limit recursion with one deepest loop and assign exact id to all elements

前端 未结 2 1302
旧巷少年郎
旧巷少年郎 2021-01-28 07:40

It is necessary to order the elements from the mixed order. Ordering is done using the recursion method. Two conditions must be met that while are not implemented in the code:

2条回答
  •  深忆病人
    2021-01-28 08:24

    Only how to choose the longest one chain?

    Consider the following much simplified example.

    IMPORTANT
    In this example it is assumed that a parent object can have at most one child object. This allows us to begin the recursion with ancestor objects (objects that do not have a parent) and work downwards. Otherwise we would have to create a separate chain for every leaf object (an object that does not have any child objects) and recurse upwards from there.

    XML

    
        
        
        
        
        
        
        
        
        
        
        
        
    
    
    
    

    XSLT 1.0 (+ node-set function)

    
    
    
    
    
    
    
        
        
            
        
        
        
            
            
                
            
        
    
    
    
        
            
            
        
    
    
    
    

    After the first pass, the $chains variable will contain:

    
      
        
      
    
    
      
        
          
            
          
        
      
    
    
      
        
          
        
      
    
    
    
    

    After sorting the chains by their length (i.e. the count of descendant objects) and selecting the longest one we will get:

    Result

    
    
      
        
          
            
          
        
      
    
    
    
    

    Hint: with recursion working downwards, it is very easy to use a template parameter to pass a common value from the ancestor to all its descendants.

    提交回复
    热议问题