Dynamic grouping on element and attributes names

前端 未结 2 881
谎友^
谎友^ 2021-01-24 20:01

I would like to categorize results from an XPath under headings by element name (and then by same attribute names). Note: XML data could be inconsistent and some elements with t

2条回答
  •  执笔经年
    2021-01-24 20:44

    This stylesheet:

    
        
        
            
                
            
        
        
            
                        
                    
            
    ElementName

    Result:

    
        
    ElementName color cute name type
    dog brown yes Frank Lab
    ElementName color cute name
    cat brown yes Fluffy
    cat brown no Lucy
    ElementName color cute name
    dog brown no Spot
    dog brown yes Rover
    ElementName color name
    cat brown Princess

    Note: This assumes that all elements having the same number of attributes have also the same attribute's name (like in your input sample).

    EDIT: Better ouput markup.

    EDIT 2: Another kind of solution: one header with all posible attribute (like CSV pattern) and order element by attribute count and name.

    
        
        
        
            
                
                            
                        
                        
                        
                    
    ElementName

    Result:

    
        
    ElementName color cute name type
    dog brown yes Frank Lab
    cat brown yes Fluffy
    cat brown no Lucy
    dog brown no Spot
    dog brown yes Rover
    cat brown Princess

    Note: This runs through the tree twice but without extension. Exact match for desired output without extensions would require to mimic key mechanism like this: run through the tree adding new keys (name of element plus attributes' names) to a param, then again for every key run through the tree filtering node by key (could be a little optimization keeping a node set for non matching elements...). Worst case (every node with distinc key) will pass trough a node: N (for key building) + (N + 1) * N / 2

提交回复
热议问题