first last element

后端 未结 1 834
不思量自难忘°
不思量自难忘° 2021-01-21 12:23

XSLT XML Question.

Looking into a simple transformation. I have simple index xml input. I have to output the the first and last element with a for each chapter. AS shown

1条回答
  •  走了就别回头了
    2021-01-21 12:44

    Updated to take advantage of a cleaner predicate - thanks to @SeanB.Durkin.

    I. XSLT 1.0 Solution

    When this XSLT:

    
    
      
      
    
      
    
      
        
          
        
      
    
      
        
      
    
    
    

    ...is applied to the original XML:

    
      Chapter01
      Chapter01
      Chapter01
      Chapter01
      Chapter01
      Chapter01
      Chapter02
      Chapter02
      Chapter02
      Chapter02
      Chapter02
      Chapter02
      Chapter03
      Chapter03
      Chapter03
      Chapter03
      Chapter03
      Chapter03
    
    

    ...the desired result is produced:

    
    
      Chapter01
      Chapter01
      Chapter02
      Chapter02
      Chapter03
      Chapter03
    
    

    Explanation:

    • Note the proper use of Muenchian Grouping to determine the unique elements by their value.
    • For each unique element, two elements are copied: the first and last from the group that contains the unique value.

    II. XSLT 2.0 Solution

    Note that in XSLT 2.0, the solution becomes even simpler.

    When this XSLT:

    
    
      
      
    
      
        
          
            
          
        
      
    
    
    

    ...is applied to the original XML, the same desired result is produced:

    
    
      Chapter01
      Chapter01
      Chapter02
      Chapter02
      Chapter03
      Chapter03
    
    

    Explanation:

    • The same methodology is applied, but instead of Muenchian Grouping, XSLT 2.0's for-each-group element and current-group() instruction are used.

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