xml - XQuery difference between "where" on a "for" clause and "where" on a "let" clause -
consider following xml:
<bib> <book year="1994"> <title>tcp/ip illustrated</title> <author><last>stevens</last><first>w.</first></author> <publisher>addison-wesley</publisher> <price>65.95</price> </book> <book year="2000"> <title>data on web</title> <author><last>abiteboul</last><first>serge</first></author> <author><last>buneman</last><first>peter</first></author> <author><last>suciu</last><first>dan</first></author> <publisher>morgan kaufmann publishers</publisher> <price>39.95</price> </book> <book year="1999"> <title>the economics of technology , content digital tv</title> <editor><last>gerbarg</last><first>darcy</first><affiliation>citi</affiliation></editor> <publisher>kluwer academic publishers</publisher> <price>129.95</price> </book> </bib>
i know following xquery query
for $book in doc(“books.xml”)//book $book/price>60 return <expensivebook> { $book/title/text() } </expensivebook>
will return title of 2 books price greater 60:
<expensivebook>tcp/ip illustrated</expensivebook> <expensivebook>the economics of technology , content digital tv</expensivebook>
instead following query:
let $books := doc(“books.xml”)//book $books/price > 60 (: existential... :) return <expensivebooks> { $books/title } </expensivebooks>
will return whole of 3 books in xml doc:
<expensivebooks> <title>tcp/ip illustrated</title> <title>data on web</title> <title>the economics of technology , content digital tv</title> </expensivebooks>
i can't understand why happens, shouldn't have whole of books variable declared "let" , filter clause? why returns "data on web" book price less 60?
another curious thing can't understand query following one:
let $books in doc(“books.xml”)//book[price>60] return <expensivebook> { $books/title } </expensivebook>
correctly returns 2 books price greater 60:
<expensivebooks> <title>tcp/ip illustrated</title> <title>the economics of technology , content digital tv</title> </expensivebooks>
how things work here? why behaviour isn't 1 expected? clause work differently on let , clauses? , last query, "let" clause used specifing price element through xpath syntax [element=value]?
thanks help, :)
i think need find basic resources on xpath , xquery. when say:
let $books := doc(“books.xml”)//book $books/price > 60 (: existential... :) return <expensivebooks> { $books/title } </expensivebooks>
what means is: bind $books sequence of book elements, consider sequence of price elements of books in sequence: if of them more 60, return sequence of title elements of of book elements in $books, wrapped in element. or none.
your first query said: iterate through sequence of book elements, , if 1 have right has price more 60, return title.
the query constraint on xpath:
let $books in doc(“books.xml”)//book[price>60] return <expensivebook> { $books/title } </expensivebook>
says: construct sequence of book elements price child element more 60, , return element consisting of sequence of titles of books, wrapped in element.
let binds variable; binds variable each member of sequence in turn. path expressions on sequences produce other sequences. '=' , '>' existential operators operate on sequences , return single boolean result.
Comments
Post a Comment