MarkLogic:XQuery从XML文档获取不同的名称? [英] MarkLogic: XQuery to Get Distinct Names from XML Document?

查看:54
本文介绍了MarkLogic:XQuery从XML文档获取不同的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下文件:

<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="en">XQuery Kick Start</title>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>Vaidyanathan Nagarajan</author>
    <year>2003</year>
    <price>49.99</price>
  </book>
  <book category="WEB">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

我正在使用以下查询从XML文档中获取名称

I am using the following query to get the names from the XML document

for $x at $i in doc("bookstore.xml")/bookstore/book/*
return fn:distinct-values(name($x))

我得到以下结果:

title
author
year
price
title
author
year
price
title
author
author
author
author
author
year
price
title
author
year
price

相反,我只想要如下所示的一种.

Instead I just want the distinct one as shown below.

title
author
year
price

我相信我搞砸了for循环.有人可以帮我解决此问题吗?我尝试使用distinct-values().没有运气.

I believe I messed up the for loop. Can someone help me with a fix for this? I tried using distinct-values(). No luck.

推荐答案

name($x)包含当前由变量$x引用的元素的名称.并且$x总是一次引用一个元素,因此在name($x)上调用distinct-values()不会有用.相反,您想在包含所有元素名称的集合上调用distinct-values(),例如:

name($x) contains name of element that currently referenced by variable $x. And $x always references one element at a time, so calling distinct-values() on name($x) won't be useful. Instead, you want to call distinct-values() on a collection containing all the elements name, like for example :

let $result := 
    for $x at $i in doc("bookstore.xml")/bookstore/book/*
    return name($x)
return fn:distinct-values($result)

这也可以使用普通的XPath表达式实现,如下所示:

This can also be achieved using plain XPath expression as follow :

distinct-values(doc("bookstore.xml")/bookstore/book/*/name(.))

这篇关于MarkLogic:XQuery从XML文档获取不同的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆