XSLT: Using variables in a key function

前端 未结 3 485
陌清茗
陌清茗 2020-12-21 13:38

I have a following XML file:


   
   

        
3条回答
  •  一个人的身影
    2020-12-21 14:14

    Rather than using variables, you could just include an element in your XSLT sheet in it's own namespace, and refer to that, like this:

    
      
    
       
        David Perry
        Jim Kim 
      
    
      
    
      
        
          
        
      
    
    

    The book template matches those that do NOT have a corresponding my:name element for their author, and outputs nothing. The identity template outputs everything else, including the book elements you DO care about. The key's a bit of a hack, it essentially matches the whole document where the name exists, rather than matching the my:name element that matches. Since you only care about it's existence, this shouldn't be a problem.

    Alternatively, if you'd rather be able to pass in a list of authors, you can use this:

    
    
      
    
      
        
          
        
      
    
      
        
          
        
      
    
    

    The variable's used in an rather than in the template match, but it does the same job. This particular code needs the list of authors specified as a comma separated list, but it should be easy enough to adapt it if you'd rather use a different format.

提交回复
热议问题