MarkLogic: XQuery to Get Distinct Names from XML Document?
<p>I am using the following file:</p>

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

<p>I am using the following query to get the names from the XML document</p>

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

<p>I am getting following results:</p>

<pre><code>title
author
year
price
title
author
year
price
title
author
author
author
author
author
year
price
title
author
year
price
</code></pre>

<p>Instead I just want the distinct one as shown below.</p>

<pre><code>title
author
year
price
</code></pre>

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