Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XQuery distinct-nodes usage

Tags:

xml

xquery

basex

So i want to insert the function distint-nodes in the for clause (see which for below). I'm using BaseX for this purpose.

This is my code:

<autores>{
  for $a in doc("libros.xml")//libro
  return 
    <autor>
    <nombre>{
      for $b in $a/autor
      return concat($b/nombre,' ',$b/apellido)
    }
    </nombre>
    {
      for $c in doc("libros.xml")//libro
      where $c/autor = $a/autor
      return $c/titulo
    }
    </autor> 

  }
</autores>

I want to use this function in the first for, so it only returns me an unique instance of the <autor/> element:

for $b in distinct-nodes($a/autor)
      return concat($b/nombre,' ',$b/apellido)

But I get the following error (BaseX Query info):

Error: Stopped at G:/Pruebas XML/XML/xqueryLibros.xq, 6/31: [XPST0017] Unknown function: fn:distinct-nodes.

Why this function is unknown when it exists? Is there something I'm missing?

EDIT: My purpose is to get an unique instance of the element $a/autor where $a/autor/nombre and $a/autor/apellidos text values are the same

<autores>
  <autor>
    <nombre>W. Stevens</nombre>
    <titulo>TCP/IP Ilustrado</titulo>
    <titulo>Programación Avanzada en el entorno Unix</titulo>
  </autor>
  <autor>
    <nombre>W. Stevens</nombre>
    <titulo>TCP/IP Ilustrado</titulo>
    <titulo>Programación Avanzada en el entorno Unix</titulo>
  </autor>
  <autor>
    <nombre>Serge Abiteboul Peter Buneman Dan Suciu</nombre>
    <titulo>Datos en la Web</titulo>
  </autor>
  <autor>
    <nombre/>
  </autor>
</autores>
like image 996
JD0001 Avatar asked Jan 05 '23 06:01

JD0001


1 Answers

There is no standard XQuery function fn:distinct-nodes(...), XQuery only knows fn:distinct-values(...).

The third-party XQuery function library functx knows a functx:dinstinct-nodes(...) function, which is again implemented as standard XQuery functions. The library can be downloaded and imported as a module with most XQuery implementations, as it only uses standard XQuery functions.

If all the <autor/> elements contains is the author's name, consider applying fn:distinct-values(...) instead and recreate the <autor/> elements afterwards.

For performance reasons, it might be reasonable to only extract the required functions if compile time increases too much (the library is rather large). Also be aware that some of the functions have much faster XQuery 3.0 counterparts, taking advantage of new language capabilities.

fn is the default XQuery function namespace, funct the one defined by the function library.

like image 173
Jens Erat Avatar answered Jan 12 '23 22:01

Jens Erat