xslt to skip already “visited” nodes

后端 未结 3 1608
故里飘歌
故里飘歌 2021-01-05 05:16

not sure if this is possible without having to go through several passes, but I\'ll ask anyway (my XSL is a little rusty)

I have an XML document, which contains node

3条回答
  •  失恋的感觉
    2021-01-05 05:41

    Ooh, this is more complicated than it looked at first. +1 for good question.

    I think the best way to accomplish this in XSLT 1.0 would be to pass an accumulating parameter whenever you apply-templates to a structure. The parameter (call it "$visited-structures") is a space-delimited list of names of structures you've already processed.

    Update: finally got this. :-)

    In the template for processing a structure, check whether any other structures this one depends on are not already listed in $visited-structures. If not, generate the code for this structure, and recurse on the template selecting the next non-visited structure, appending the current structure name to the $visited-structures parameter. Otherwise, don't generate code for the structure but recurse on the template selecting the first dependency structure, passing the $visited-structures parameter unmodified.

    Here is the code...

    
    
       
    
       
          
             
             
          
       
    
       
            
          
          
             
                
                   
                            
             
             
                
    struct 
    {
    };
                
                
                   
                
             
                
       
    
       
                
       
    
    
    

    And the output:

    
    
    struct STRUCT_C
    {
      FIELD_E e;
      FIELD_F f;
      FIELD_G g;
    };
    
    
    struct STRUCT_B
    {
      STRUCT_C c;
      FIELD_E e;
    };
    
    
    struct STRUCT_A
    {
      STRUCT_B b;
      STRUCT_C c;
      FIELD_D d;
    };
    

提交回复
热议问题