XSLT grouping on multiple keys using Muenchian method

前端 未结 1 462
灰色年华
灰色年华 2020-12-06 15:55

This is the input file.

All these blocks are wrapped in a tag which is not appearing, dunno why? And all these blocks are wrapped in a

1条回答
  •  执念已碎
    2020-12-06 16:30

    If the hd1 element is always '1234' then you are not really grouping by them, but if you were you would define a simple key like so

    
    

    For the second key, you would need to take account of the Code element

    
    

    And then for the last key, you would define a more complicated key to cope with all the elements

    
    

    Do note the use of the 'pipe' character as the delimiter. It is important to pick a delimited that would never occur in any of the selected elements.

    Then, to look for the distinct header1 elements, you would look for the elements which appear first in the header1 key

    
    

    To find the distinct Code elements within each header1 element, you would do the following

     
    

    Finally, within each code group, to find the distinct 'header3' elements, you would look for the first elements within the third key

     
    

    Here is the full XSLT

    
       
    
       
       
       
    
       
          
       
    
       
          
             
                
             
             
          
       
    
       
          
             
             
          
       
    
       
          
             
             
          
       
    
       
          
             
          
       
    
    

    Do note the use of the mode attribute on the template matching to distinguish between the multiple templates all matching the AllocFile elements.

    When applied to your sample XML, the following is output

    
       1234
       
          033195
          
             147
             125487
             11
             9
             
                1
                11/10
                
    20090401
    8000000
    2 14/10
    20090401
    8400000
    3 74/10
    20090401
    8740000
    033118 117 125487 19 9 2 74/10
    20090401
    74512
    033147 117 125487 19 9 3 14/10
    20090401
    986541

    If you did have different hd1 elements, other than '1234' you would end up with multiple Header1 elements, and so your output would not be well-formed XML. It would be simple enough to wrap them in a root element though by modified the initial template matching the document element.

    
       
          
       
    
    

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