XQuery: Simple query example -
i have assignment create xml file , query using xquery. file looks this:
<library> <book> <title>title1</title> <author>author1</author> <author>author2</author> <subject>subject1</subject> </book> <book> <title>title2</title> <author>author3</author> <subject>subject1</subject> </book> </library>
for each author, i'm supposed return title of book, title 1 should returned twice.
i've been trying this:
let $a:=doc("/home/user/library.xml")/library/book $x in $a/title, $y in $a/author return $x
the results i'm getting are: title1 title2 title1 title2 title1 title2
i see problem every author, i'm returning every book title, i'm not sure how return book titles correspond particular author. suggestions?
thanks!
use:
$author in distinct-values(/*/*/author) return /*/book[$author = author]/title/string()
when xpath expression (that xquery, because xpath subset of xquery) evaluated against provided xml document:
<library> <book> <title>title1</title> <author>author1</author> <author>author2</author> <subject>subject1</subject> </book> <book> <title>title2</title> <author>author3</author> <subject>subject1</subject> </book> </library>
the wanted, correct result produced:
title1 title1 title2
Comments
Post a Comment