Creating a nested tree structure from a path in XSLT

后端 未结 1 661

I have a dynamic XML document which represents a tree structure of categories, but does so using path separated attributes in arbitrary order - like this:

1条回答
  •  -上瘾入骨i
    2020-12-16 21:47

    Sure, no problem:

    
    
      
    
      
        
        
          
          
        
      
    
      
        
    
         
        
          
    
          
          
            
          
    
          
          
    
          
          
    
          
          
            
          
        
      
    
    
    

    Output on my system:

    
      
        
          
          
        
      
    
    

    Note that the whole solution is based on the assumption that all the paths are canonical and do not contain trailing backslashes.

    Also note that any unmatched/orphaned "record" elements will not be in the output (unless they are at the root level, of course).

    One more thing: The template mode ("recurse") is not strictly necessary. I included it because the template is doing something rather special, and there might be the chance that there is another template in your solution that matches "record" nodes. In this case this solution can be dropped in without breaking anything else. For a standalone solution, the template modes can be dropped anytime.

    Oh, and the last thing: If you want the result document to be ordered by Name, include an element with the (both occurrences), like so:

    
      
    
    

    0 讨论(0)
提交回复
热议问题