[xquery-talk] distinct-values in Where Clauses
Ronald Bourret
rpbourret at rpbourret.com
Thu Jan 17 21:29:04 PST 2008
I'm not entirely sure what it is you want, but I have a few comments:
1) Why are you using nested for loops? The following clause:
for $find in doc("resume.xml")//div, $head in $find/head, $desc in
$find/p
do something
is similar to:
for i=1 to 10
for j=1 to 20
for k=1 to 30
do something
That is, the code finds each div. For each div, it finds all the head
children. And for each head child, it finds all the p children. It then
executes the return clause once for each p child. (A clue that this is
not intended is that p is a child of div, not head, which makes for
inobvious nested loops.)
It seems more likely that you want something like:
for $div in doc("resume.xml")//div
let $heads := $find/head
let $paragraphs := $find/p
...
2) It is not clear why you are using distinct-values. Based on your
sample below, it appears that you want to repeat the head and p children
of each div that contains $text in a head or a p. Perhaps you want
something like:
for $div in doc("resume.xml")//div
let $heads := $div/head
let $paragraphs := $div/p
where some $head in $heads satisfies fn:contains($head, $text) or
some $p in $paragraphs satisfies fn:contains($p, $text)
return
<div>
{
$heads,
$paragraphs
}
3) Your example below contains a div and a note. It appears that you
want to change the note to a div. However, the following path expression:
for $div in doc("resume.xml")//div
will not retrieve any note elements. Is this a typo? If not, perhaps you
want:
let $resume := doc("resume.xml")
for $div_or_note in $resume//div union $resume//note
let $heads := $div_or_note/head
let $paragraphs := $div_or_note/p
where some $head in $heads satisfies fn:contains($head, $text) or
some $p in $paragraphs satisfies fn:contains($p, $text)
return
<div>
{
$heads,
$paragraphs
}
-- Ron
Wei, Alice J. wrote:
> Here is the XQuery:
>
> declare boundary-space preserve;
> declare variable $text external;
> <text>{
> for $find in doc("resume.xml")//div,
> $head in $find/head,
> $desc in $find/p
> where
> distinct-values((contains($desc,$text)) or (contains($head,$text)))
> return <div> <head>{data($head)}</head> <p>{data($desc)}</p> </div>
> } </text>
>
> XML:
>
> <div>
> <head>PHP and MySQL</head>
> <p>So far I have not seen full support of XQuery usage here.</p>
> </div>
>
> <note>
> <head>PHP5</head>
> <p>XML nor XQuery does not seem to be even available.</p>
> </note>
>
> It appears that my XML is well-formed. What I really want to see from the result is:
>
> <div>
> <head>PHP and MySQL</head>
> <p>So far I have not seen full support of XQuery usage here.</p>
> </div>
>
> <div>
> <head>PHP5</head>
> <p>XML nor XQuery does not seem to be even available.</p>
> </div>
>
> This is, as a matter of fact, only a small portion of the output. Since I am calling this from an external variable, some of the files that generate a longer result jeopardizes the output.
>
> If I set the $text variable to XQuery, the result is supposed to be
> appearing only once for each, but now it gives me two results for each. Can
> anyone help out?
>
> Thanks to those who can help.
More information about the talk
mailing list