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

David Carlisle davidc at nag.co.uk
Thu Apr 3 10:06:33 PST 2008



>  Here is the code of my main function:

What do you intend this function to do? it's very hard for me to guess
because most of the lines of it do  nothing.

$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)],
$e := collection("my")//ad//head[contains(upper-case(.),$search)],
$f := collection("my")//ad//head[contains(upper-case(.),$search5)],
$g := collection("my")//ad/head[contains(upper-case(.),$search6)],
$h := collection("my")//ad//p[contains(upper-case(.),$search6)],
$i:= collection("my")//ad//address[contains(upper-case(.),$search6)]

none of the above are used, so could be removed.


let $search := upper-case(request:get-parameter("search", ""))
let $search2 := request:get-parameter("mydropdown", "")
let $search3 := request:get-parameter("mydropdown2", "")
let $search4 := request:get-parameter("mydropdown3", "")
let $search5 := upper-case(request:get-parameter("search2", ""))
let $search6 := upper-case(request:get-parameter("search3", ""))
let $search7 := request:get-parameter("restriction", "")
let $search8 := request:get-parameter("restriction2", "")



all of the above are only used in the definition of $a,... so they could
be removed as well.



let $pb := $seq[.=$d][1]/preceding::pb[1]

that one isn't used either

In this version you have removed the iteratioon

for $d at $count in $sorted_result

so you are not iterating through the elements at all, you jusht ave some
let statements in your outer FLWOR so there is nothing to sort so 
order by $d
can do nothing, sorting a sequence of 1.


You need to keep the code inside the for loop not just move the for
statement to the end.



if (count($sorted_result) lt 1)
then
<p>Sorry, too many results returned.

You still have that test backwards, saying "too many" for a list of
length 0 and "sorry no results" if the list is too long


<p>{$para}</p>

you still have that, which will make p inside p, rather than 
just ($para}


So having fixed all those (again)  here's a coomplet query that runs.

declare function local:searchresult($seq as element()*) as element()*
{
let  $sorted_result:=
        for $doc in distinct-values($seq)
        order by $doc
        return $doc
return
if (count($sorted_result) lt 1)
then
<p>Sorry, there are no results retrieved back. Please check for your input. </p>
else if (count($sorted_result) gt 3)
then
<p>Sorry, too many results returned. Please refine your search to have them displayed.</p>
else
for $d at $count in $sorted_result
let $head := $seq[.=$d][1]/ancestor::ad/descendant::head[1]
let $head2 := $seq[.=$d][1]/ancestor::ad/descendant::head[2]
let $head3 := $seq[.=$d][1]/ancestor::ad/descendant::head[3]
let $para := $seq[.=$d][1]/ancestor::ad/descendant::p
let $note := $seq[.=$d][1]/ancestor::ad/descendant::note
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>
{$para}
</div>
};


(

local:searchresult((
<ad><head>a</head></ad>/head
,
<ad><head>b</head></ad>/head
))

,

local:searchresult(()))


,

local:searchresult((
<ad><head>a</head></ad>/head
,
<ad><head>b</head></ad>/head
,
<ad><head>c</head></ad>/head
,
<ad><head>d</head></ad>/head
))









That produces the following, for the three calls to the function.

$ saxon9q alice3.xq
<?xml version="1.0" encoding="UTF-8"?>
<div>
   <p>Showing Result: <b>1 / 2</b>
   </p>
   <p style="font-weight:bold">
      <head>a</head>    </p>
</div>
<div>
   <p>Showing Result: <b>2 / 2</b>
   </p>
   <p style="font-weight:bold">
      <head>b</head>    </p>
</div>
<p>Sorry, there are no results retrieved back. Please check for your input. </p>
<p>Sorry, too many results returned. Please refine your search to have them displayed.</p>



> As for the distinct-values in my code, I always have the assumption
> that it has been generating only results with distinct values as long
> as it is not deep distinct. 

I have no idea what you mean by that.
distinct-values returms the distinct string values

If you have a sequence of p items that looks like this

<p>
Send 10c for mailing TODAY!
                                <address>
<addressLine>Kenmore, Mitford, CR-272 N.H. 03055</addressLine>
</address>
</p>

then the string that is calcultad and compared is

"
Send 10c for mailing TODAY!
                                
Kenmore, Mitford, CR-272 N.H. 03055

"

which seems a pretty odd thing to do, but I don't know your
requirements.

David

________________________________________________________________________
The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs. 
________________________________________________________________________


More information about the talk mailing list