How to find the max attribute from an XML document using Xpath 1.0

后端 未结 7 1043
无人及你
无人及你 2020-11-29 10:43

Is there a way to query an XML document to return the maximum of a given attribute using Xpath 1.0 ?

For example is there a way to get the max id ?

&         


        
7条回答
  •  醉梦人生
    2020-11-29 10:47

    XPath 1.0

    /library/book[not(@id < /library/book/@id)]
    

    This query style is more generic and works even if books are grouped i.e.

    
    
        
            
            
        
        
            
            
        
    
    

    Same query still works (the path should be modified)

    /library/genre/book[not(@id < /library/genre/book/@id)]
    

    or even

    //book[not(@id < //book/@id)]
    

    To avoid performance troubles use XPath 2 max() instead

提交回复
热议问题