XQuery:简单查询示例
我的任务是创建一个xml文件并使用XQuery查询它。该文件大致如下所示:XQuery:简单查询示例,xquery,Xquery,我的任务是创建一个xml文件并使用XQuery查询它。该文件大致如下所示: <library> <book> <title>Title1</title> <author>Author1</author> <author>Author2</author> <subject>Subject1</subject>
<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>
let $a:=doc("/home/user/library.xml")/library/book
for $x in $a/title, $y in $a/author
return $x
我得到的结果是:
标题1
标题2
标题1
标题2
标题1
标题2
我发现问题在于,对于每个作者,我都会返回每个书名,但我不确定如何让它只返回与特定作者对应的书名。有什么建议吗
谢谢 使用:
for $author in distinct-values(/*/*/author)
return
/*/book[$author = author]/title/string()
<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>
根据提供的XML文档计算此XPath表达式(也就是XQuery,因为XPath是XQuery的子集)时:
for $author in distinct-values(/*/*/author)
return
/*/book[$author = author]/title/string()
<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>
谢谢,这真的很有帮助。我忘了如何正确使用XPath导航了。@coffeeNjava,不客气。我建议从一本关于XPath的好书开始。