How to call named templates based on a variable?

后端 未结 6 497
一生所求
一生所求 2020-12-15 23:53

I don\'t know if it\'s possible, but I\'m wondering how to do it...

Let\'s say we have the following XSL:


  Bla bla         


        
6条回答
  •  鱼传尺愫
    2020-12-16 00:23

    It's not possible exactly as you describe, but if you want to be able to choose a template at run-time based on some value you set elsewhere, there is a trick to do that. The idea is to have your named template also match a node with a corresponding name in a distinct mode (so that it doesn't mess up your normal transformation), and then match on that. For example:

    
    
      
      
        
        
      
    
           
      
    
      
        Bla bla bla
      
    
      
        Bla bla bla
      
    
      
        
        
      
    

    Note that you can use in , so you can do everything with this that you could do with a plain .

    Also, the code above is somewhat lengthier than you might need because it tries to avoid using any XSLT extensions. If your processor supports exslt:node-set(), then you can just generate nodes directly using , and use node-set() to convert the resulting tree fragment to a plain node to match against, without the need for document('') hack.

    For more information, see FXSL - it's a functional programming library for XSLT that is based on this concept.

提交回复
热议问题