XSLT - How to keep only wanted elements from XML

前端 未结 2 713
青春惊慌失措
青春惊慌失措 2020-12-03 00:28

I have a number of XML files containing lots of overhead. I wish to keep only about 20 specific elements and filter out anything else. I know all the names of the elements I

2条回答
  •  攒了一身酷
    2020-12-03 00:45

    This general transformation:

    
     
     
    
     
      ns:currency
      ns:currency_code3
     
    
     
         
           
         
     
    
     
    
    

    when applied on the provided XML document (with namespace definition added to make it well-formed):

    
        
            somecurrency
            
            
            
            
        
    
    

    produces the wanted result (white-listed elements and their structural relations are preserved):

    
       
          somecurrency
          
       
    
    

    Explanation:

    1. The identity rule/template copies all nodes "as-is".

    2. The stylesheet contains a top-level element whose children specify all white-listed element's names -- the elements that are to be preserved with their structural relationships in the document.

    3. The element is best kept in a separate document so that the current stylesheet will not need to be edited with new names. Here the whitelist is in the same stylesheet just for convenience.

    4. One single template is overriding the identity template. It doesn't process (deletes) any element that is not white-listed and has no descendent that is white-listed.

提交回复
热议问题