[xquery-talk] Query Through Multiple Files

Wei, Alice J. ajwei at indiana.edu
Mon Jan 21 14:58:30 PST 2008


Hi,


   I am wondering if there is a secondary "alternative" to outputting results that may be duplicates. I tried using distinct-values(doc("1.xml"), doc("3.xml"), doc("4.xml"),  doc("2.xml"))//title, but I got the error that there should be only 2 arguments. or less. Is there some way of which I could avoid duplicate values in my query performance as my results shown below:

My XQuery:

  declare boundary-space preserve;
declare variable $text external;

<project>
{
let $sorted_result:= for $book in
(doc("1.xml"), doc("3.xml"), doc("4.xml"),  doc("2.xml"))//title,
$note in $book/note,
$head in $book/head
where
distinct-values(contains($desc,$text) or contains($head,$text))
return $book
for $sorted_results at $count in $sorted_result
return
<book>
<statistics>Showing Result: {$count} / {count($sorted_result)}
</statistics>
{$sorted_result/head}
{$sorted_result/p}
</book>
}
</project>

Current output:

<book>
    <statistics>Showing Result: 1 / 4</statistics>
     <head>XQuery</head>
    <p>This book provides: A high-level overview and quick tour of XQuery Information to write sophisticated queries, without being bogged down by the details of types, namespaces, and schemas.</p>
</book>
<book>
    <statistics>Showing Result: 2 / 4</statistics>
     <head>XQuery</head>
    <p>This book provides: A high-level overview and quick tour of XQuery Information to write sophisticated queries, without being bogged down by the details of types, namespaces, and schemas.</p>
</book>
<book>
    <statistics>Showing Result: 3 / 4</statistics>
     <head>XQuery</head>
    <p>This book provides: A high-level overview and quick tour of XQuery Information to write sophisticated queries, without being bogged down by the details of types, namespaces, and schemas.</p>
</book>
<book>
    <statistics>Showing Result: 4 / 4</statistics>
     <head>XQuery</head>
    <p>This book provides: A high-level overview and quick tour of XQuery Information to write sophisticated queries, without being bogged down by the details of types, namespaces, and schemas.</p>
</book>

Intended Output:
<book>
    <statistics>Showing Result: 1/1</statistics>
     <head>XQuery</head>
    <p>This book provides: A high-level overview and quick tour of XQuery Information to write sophisticated queries, without being bogged down by the details of types, namespaces, and schemas.</p>
</book>

Thanks to those who can help.
======================================================
Alice Wei
MIS 2008
School of Library and Information Science
Indiana University Bloomington
ajwei at indiana.edu
________________________________________
From: Michael Kay [mike at saxonica.com]
Sent: Sunday, January 20, 2008 8:07 PM
To: 'Liam Quin'; Wei, Alice J.
Cc: talk at x-query.com
Subject: RE: [xquery-talk] Query Through Multiple Files

> >Alice: I did use Liam's suggestions,
> >
> > for $ad in (
> >        doc("1.xml"),
> >        doc("2.xml"),
> >        doc("3.xml"),
> >        doc("4.xml"))//ad
> >
> Liam:That wasn't exactly what I suggested... :-)
>
> XQuery is not a good language in which to make random guesses
> and hope things will work.
>
> You can try,
> for $doc in (
>        doc("1.xml"),
>        doc("2.xml"),
>        doc("3.xml"),
>        doc("4.xml"))
>     return $doc//ad
>

or more simply,

( doc("1.xml"),
  doc("2.xml"),
  doc("3.xml"),
  doc("4.xml"))//ad

or if you want to eliminate duplicates,

distinct-values(( doc("1.xml"),
  doc("2.xml"),
  doc("3.xml"),
  doc("4.xml"))//ad)

I've found when teaching XQuery that there are two important
misunderstandings that students easily make:

(a) the idea that every query has a FLWOR expression as its outermost
construct

(b) the idea that a FLWOR expression can only be used at the outermost level
of a query

Michael Kay




More information about the talk mailing list