[英]MarkLogic: XQuery to Get Values from XML Documents?
I have the following XML document loaded in MarkLogic database: 我在MarkLogic数据库中加载了以下XML文档:
<x:books xmlns:x="urn:books">
<book id="bk001">
<author>Writer</author>
<title>The First Book</title>
<genre>Fiction</genre>
<price>44.95</price>
<pub_date>2000-10-01</pub_date>
<review>An amazing story of nothing.</review>
</book>
<book id="bk002">
<author>Poet</author>
<title>The Poet's First Poem</title>
<genre>Poem</genre>
<price>24.95</price>
<review>Least poetic poems.</review>
</book>
</x:books>
I am new to XQuery. 我是XQuery的新手。 How would I retrieve the values from the XML document as I retrieve it from a SQL database?
当我从SQL数据库中检索它时,如何从XML文档中检索这些值?
Output: 输出:
BookID | Author | Title | Genre | price | pub_date | review
bk001 | Writer | The First Book | Fiction | 44.95 | 2000-10-01
bk002 | Poet | The Poet's First Poem | Poem | 24.95 | Least poetic poems.
Note: Not necessary a pipe delimited but some collection list. 注意:不需要管道分隔但有些集合列表。
Can some one share some link or help me write this XQuery? 有人可以分享一些链接或帮我写这个XQuery吗? I am new to this.
我是新来的。
XQuery's sequence construct will hold multiple values, but it's not hierarchical - so if you create a sequence of sequences, it will simply join them all together into one large sequence. XQuery的序列构造将包含多个值,但它不是分层的 - 所以如果你创建一个序列序列,它将简单地将它们连接成一个大序列。
This will capture all child element and attribute values into a sequence, but because of the property of sequences I just mentioned, there would be no built in way to get the first value of the second book. 这会将所有子元素和属性值捕获到一个序列中,但由于我刚才提到的序列的属性,没有内置的方法来获取第二本书的第一个值。 You would have to know that it's the 7th item.
你必须知道这是第7项。 And that the first value of a third book would be the 14th item, and so on:
并且第三本书的第一个值将是第14个项目,依此类推:
$books/book/(*|@*)/string()
Just to demonstrate how you would achieve a pipe delimited list: 只是为了演示如何实现管道分隔列表:
string-join($books/book[1]/(*|@*)/node-name() ! string(), ' | '), (: Create header row :)
for $book in $books/book
return string-join($book/(*|@*)/string(), ' | ')
@wst: Thank you so much. @wst:非常感谢你。 For some reason i couldn't run the same in marklogic.
出于某种原因,我无法在marklogic中运行相同的内容。 May be it works for generic XML Xquery.
可能适用于通用XML Xquery。 But I found the following solution
但我找到了以下解决方案
for $x at $i in doc("bookstore.xml")/bookstore/book
return data($x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.