Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

<?xml version="1.0" encoding="utf-8"?>
<library>
        <book id="2" name="Dragon Tatoo"/>
        <book id="7" name="Ender's Game"/>
        <book id="3" name="Catch 22"/>
        <book id="1" name="Lord of the rings"/>
</library>
like image 768
HerbSpiral Avatar asked Jan 02 '12 14:01

HerbSpiral


People also ask

How selecting XML data is possible with XPath explain?

XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the expressions you see when you work with a traditional computer file system. XPath expressions can be used in JavaScript, Java, XML Schema, PHP, Python, C and C++, and lots of other languages.


1 Answers

In XPath 2.0, use the max function. To find the book with the highest id, do

/library/book[@id = max(/library/book/@id)]
like image 125
Fred Foo Avatar answered Sep 20 '22 18:09

Fred Foo