[xquery-talk] If Statements within an If Statement?

Wei, Alice J. ajwei at indiana.edu
Wed Apr 2 13:24:54 PST 2008


Hi, Michael:

  Here is the code with some data:

declare function local:searchresult($seq as element()*) as element()*
{
let $a := <ad>
<head type="sub">(Send 13 cent stamp) </head>
<p rend="bgyellow"><address>
<addressLine>The American Comic Co.</addressLine>
<addressLine>12206 Ventura Blvd., </addressLine>
<addressLine>Studio City, Cal. 91604</addressLine>
</address>
</p>
</ad>,
$b := <ad><head type="sub">
<emph rend="red">55 Red</emph>
 China Stamps banned in America!
</head>
<p>
Get these valuable red China old and new postage stamps—forbidden
                        for a generation to America collectors by the U.S. Gov't. At last you can
                        legally own them—you may never have another chance for supplies
                        are strictly limited so act fast! This spectacular and genuine offer given
                        to introduce you to Kenmore's sensational world-wide stamps ON-Approval
                        Service for Free Examination. Buy $2 or more and get other valuable bonuses.
                        Or buy nothing—return selections—cancel service anytime.
                        In either case, the 55 red China stamps are yours to keep FREE.
</p>
        <p>
Send 10c for mailing TODAY!
                                <address>
<addressLine>Kenmore, Mitford, CR-272 N.H. 03055</addressLine>
</address>
</p></ad>

let $sorted_result:=
for $doc in distinct-values($seq)
order by $doc
return $doc
for $d at $count in $sorted_result
let $head := $seq[.=$d][1]/ancestor::ad//child::head[1]
let $head2 := $seq[.=$d][1]/ancestor::ad//child::head[2]
let $head3 := $seq[.=$d][1]/ancestor::ad//child::head[3]
let $para := $seq[.=$d][1]/ancestor::ad//child::p
order by $d
return
if (count($sorted_result) lt 1)
then
<p>Sorry, no results were retrieved.</p>
else if (count($sorted_result) gt 20)
then
<p>Sorry, too many results were retrieved.</p>
else
<div>&#160;&#160;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;
<p>Showing Result: <b>{$count} / {count($sorted_result)}</b></p>
<p style="font-weight:bold">{$head}&#160; {$head2}&#160; {$head3}</p>
<p>{$para}</p>
</div>
};

let $a := <ad>
<head type="sub">(Send 13 cent stamp) </head>
<p rend="bgyellow"><address>
<addressLine>The American Comic Co.</addressLine>
<addressLine>12206 Ventura Blvd., </addressLine>
<addressLine>Studio City, Cal. 91604</addressLine>
</address>
</p>
</ad>,
$b := <ad><head type="sub">
<emph rend="red">55 Red</emph>
 China Stamps banned in America!
</head>
<p>
Get these valuable red China old and new postage stamps—forbidden
                        for a generation to America collectors by the U.S. Gov't. At last you can
                        legally own them—you may never have another chance for supplies
                        are strictly limited so act fast! This spectacular and genuine offer given
                        to introduce you to Kenmore's sensational world-wide stamps ON-Approval
                        Service for Free Examination. Buy $2 or more and get other valuable bonuses.
                        Or buy nothing—return selections—cancel service anytime.
                        In either case, the 55 red China stamps are yours to keep FREE.
</p>
        <p>
Send 10c for mailing TODAY!
                                <address>
<addressLine>Kenmore, Mitford, CR-272 N.H. 03055</addressLine>
</address>
</p></ad>
return
local:searchresult($a union $b)


Yes, like you said, it does return something , but there is no data in it except for what I have put there, as in the following:

<div>
<p>Showing Result:
<b>1 / 2</b>
</p>
<p style="font-weight:bold">    </p>
<p/>
</div>

Has something tuppled in this process? Without the if else statements, my data used to return everything according to what I defined.

Thanks for your help.

Alice
======================================================
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: Wednesday, April 02, 2008 10:43 AM
To: Wei, Alice J.; talk at x-query.com
Subject: RE: [xquery-talk] If Statements within an If Statement?

As far as I can see your syntax is OK and your if expression should always
return something. If it doesn't return anything, then perhaps it isn't being
executed, which could be because your FLOWR expression is selecting no
tuples.

Of course, you haven't provided enough information for me to test it, and
the fact that I can't see an error when scanning it by eye doesn't mean that
there is no error.

Michael Kay
http://www.saxonica.com/

> -----Original Message-----
> From: talk-bounces at x-query.com
> [mailto:talk-bounces at x-query.com] On Behalf Of Wei, Alice J.
> Sent: 02 April 2008 21:20
> To: talk at x-query.com
> Subject: [xquery-talk] If Statements within an If Statement?
>
> Hi, XQueriers:
>
>    This may be a simple problem, but it appears that I have
> either missed something or have problems figuring out what a
> certain function means.
>
> Here is my function:
>
> declare function local:searchresult($seq as element()*) as
> element()* {
>
> let $a :=
> collection("my")//ad//address[contains(upper-case(.),$search)],
> $b := collection("my")//ad//p[contains(upper-case(.),$search)],
> $c := collection("my")//ad//address[contains(upper-case(.),$search5)],
> $d := collection("my")//ad//p[contains(upper-case(.),$search5)]
>
> let $sorted_result:=
> for $doc in distinct-values($seq)
> order by $doc
> return $doc
> for $d at $count in $sorted_result
> let $head := $seq[.=$d][1]/ancestor::ad//child::head[1]
> let $head2 := $seq[.=$d][1]/ancestor::ad//child::head[2]
> let $head3 := $seq[.=$d][1]/ancestor::ad//child::head[3]
> let $para := $seq[.=$d][1]/ancestor::ad//child::p
> order by $d
> return
> <div><p>Showing Result: <b>{$count} /
> {count($sorted_result)}</b></p> <p
> style="font-weight:bold">{$head}&#160; {$head2}&#160;
> {$head3}</p> <p>{$para}</p> </div> }; local:searchresult($a union $d
>
> I would like to have this so that it can say something like
>
> return
>
> if (count($sorted_result) lt 1)
> then <p>Sorry, no results retrieved.</p> else if
> (count($sorted_result) gt 200) then <p>Sorry, too many
> results returned.</p> else <div><p>Showing Result:
> <b>{$count} / {count($sorted_result)}</b></p> <p
> style="font-weight:bold">{$head}&#160; {$head2}&#160;
> {$head3}</p> <p>{$para}</p> </div>
>
> My problem is
>
> (1) When I tried to do a count of the result in the first if
> statement, it does not even show anything. (Is this another
> XPath problem?)
> (2) Is this syntax accurate?
>
> Any suggestion is appreciated.
>
> Alice
> ======================================================
> Alice Wei
> MIS 2008
> School of Library and Information Science Indiana University
> Bloomington ajwei at indiana.edu
>
> _______________________________________________
> talk at x-query.com
> http://x-query.com/mailman/listinfo/talk




More information about the talk mailing list