Generate/get xpath from XML node java

前端 未结 8 799
清酒与你
清酒与你 2020-11-22 10:39

I\'m interested in advice/pseudocode code/explanation rather than actual implementation.

  • I\'d like to go trough xml document
8条回答
  •  旧巷少年郎
    2020-11-22 11:13

    Update:

    @c0mrade has updated his question. Here is a solution to it:

    This XSLT transformation:

    
        
        
    
        '
    
        
          
             
             
             
    
            
            
        
    
        
            
            
            
                
            
        
    
        
            
            
            
    
        
    
    

    when applied on the provided XML document:

    
        one
        two
        three
        four
        
            five
        
    
    

    produces exactly the wanted, correct result:

    /root/elemA='one'
    /root/elemA[2]='two'
    /root/elemA[2][@attribute1='first']
    /root/elemA[2][@attribute2='second']
    /root/elemB='three'
    /root/elemA[3]='four'
    /root/elemC/elemB='five'
    

    When applied to the newly-provided document by @c0mrade:

    
        
            89734
        
    
    

    again the correct result is produced:

    /root/elemX='89734'
    /root/elemX[@serial='kefw90234kf2esda9231']
    

    Explanation:

    • Only elements that have no children elements, or have attributes are matched and processed.

    • For any such element, if it doesn't have children-elements all of its ancestor-or self elements are processed in a specific mode, named 'path'. Then the "='theValue'" part is output and then a NL character.

    • All attributes of the matched element are then processed.

    • Then finally, templates are applied to all children-elements.

    • Processing an element in the 'path' mode is simple: A / character and the name of the element are output. Then, if there are preceding siblings with the same name, a "[numPrecSiblings+1]` part is output.

    • Processing of attributes is simple: First all ancestor-or-self:: elements of its parent are processed in 'path' mode, then the [attrName=attrValue] part is output, followed by a NL character.

    Do note:

    • Names that are in a namespace are displayed without any problem and in their initial readable form.

    • To aid readability, an index of [1] is never displayed.


    Below is my initial answer (may be ignored)

    Here is a pure XSLT 1.0 solution:

    Below is a sample xml document and a stylesheet that takes a node-set parameter and produces one valid XPath expression for every member-node.

    stylesheet (buildPath.xsl):


    
    
    
    
    
      
        
        
        
          /
          
                       
              
                
                
                
                
                  
                
              
            
             
              
                   
                
                  
                    
                  
                     
                  
                  
                    
                    
                    
                    
                      
                    
                  
                     
                
                
                  
                    
                    
                    
                    
                      
                    
                  
                     
                   
                  
                    
                    
                    
                    
                      
                    
                  
                     
                
                
    
                  '
                  
                    
    
                  
                     
              
                        
          
        
        
    
      
     
     
    
    
    

    xml source (buildPath.xml):


    
    
        textA
     
      
            xxxxxxxx
      
            
      
       
      
            
            yyyyyyy
      
      
        
    
    
    

    Result:

    /root/nodeA[2]/nodeB[2]/namespace::*[local-name() = 'myNamespace']
    /root/nodeA[2]/nodeB[2]/nodeC/namespace::*[local-name() =
    'myNamespace']
    

提交回复
热议问题