problem with xpath results
i'm trying to get the maximal book value (attached dtd) but i could only get the value not the node itself i used this command
max(//price)
but i got the value not the node.
this is the dtd
<!ELEMENT iventory (book)+>
<!ELEMENT book (title,author+,publisher+,price,chapter*)>
<!ATTLIST book num ID #REQUIRED>
<!ELEMENT chapter (title,(paragraph*|section))>
<!ELEMENT section (title?,paragraph*)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT au开发者_C百科thor (#PCDATA)>
<!ELEMENT publisher (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST price currency CDATA #FIXED "usd">
<!ELEMENT paragraph (#PCDATA|emph|image)*>
<!ELEMENT emph (#PCDATA)>
<!ELEMENT image EMPTY>
<!ATTLIST image file CDATA #REQUIRED
height CDATA #IMPLIED
width CDATA #IMPLIED>
another thing i need to find is all the books that their price is higher then the book preceding book, i tried many ways but hoe can i get the next node?
Do you use XPath 1.0 or 2.0? max
is XPath 2.0, isn't it? And often XPath 2.0 implementations also support XQuery 1.0 so that way you could simply do
let $max-price := max(/inventory/book/price)
return /inventory/book[price = $max-price]
to get the or those book elements having the maximum price.
If you need to use pure XPath 2.0 then of course
/inventory/book[price = max(../book/price)]
should do, it might be inefficient.
As for your second question, does
/inventory/book[price > preceding-sibling::book[1]/price]
do what you want?
With another approach, you could use this XPath 2.0 expression:
/inventory/book[index-of(/inventory/book/price,max(/inventory/book/price))[1]]
精彩评论