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

后端 未结 7 1046
无人及你
无人及你 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 11:06

    I've found that answers like the lwburk's or timbooo's work fine for attributes representing numbers having just one digit. However, if the attribute is a number having more than one digit, extrange things seem to happen when comparing between attributes' values. For example, try changing the original XML data with something like this:

    
    
            
            
            
            
    
    

    Running the suggested snippets won't work. I got a solution using the casting operator xs:int() applied on id attribute, like in:

    /library/book[not(xs:int(@id) <= preceding-sibling::book/@id) and not(xs:int(@id) <=following-sibling::book/@id)]
    

    That will give the correct answer!

提交回复
热议问题