Discussion:
[MarkLogic Dev General] query on If else
Pragya Kapoor
2010-05-19 13:37:03 UTC
Permalink
Hi,
I have a query regarding the if-else statement in query.the code below describes the query:

let $entryid="1"
let $entry1:="1"
let $entry2:="2"

for $entry in doc("uri")//root/entry
where $entry/@id = $entryid
return
if (empty($entry))
then
$entry1
else
$entry2

Now if there is a entry with entryid=1 then it gives $entry2=2 . whereas if no entry exists with entryid=1 then it gives "no results". Please suggest me some other way to check the if condition so that I can get the desired result.

Thanks,
Pragya
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://developer.marklogic.com/pipermail/general/attachments/20100519/a2375c04/attachment.html
Lee, David
2010-05-19 13:43:10 UTC
Permalink
Your where clause is guarenteeing that the else condition will never
occur.

Try this instead, it might be closer to what you want





for $entry in doc("uri")//root/entry

let $entry1_exists = $entry[@id = $entryid]

return

if (empty($entry1_exists))

then

$entry1

else

$entry2











From: general-bounces at developer.marklogic.com
[mailto:general-bounces at developer.marklogic.com] On Behalf Of Pragya
Kapoor
Sent: Wednesday, May 19, 2010 9:37 AM
To: general at developer.marklogic.com
Subject: [MarkLogic Dev General] query on If else



Hi,

I have a query regarding the if-else statement in query.the code below
describes the query:



let $entryid="1"

let $entry1:="1"

let $entry2:="2"



for $entry in doc("uri")//root/entry

where $entry/@id = $entryid

return

if (empty($entry))

then

$entry1

else

$entry2



Now if there is a entry with entryid=1 then it gives $entry2=2 . whereas
if no entry exists with entryid=1 then it gives "no results". Please
suggest me some other way to check the if condition so that I can get
the desired result.



Thanks,

Pragya

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://developer.marklogic.com/pipermail/general/attachments/20100519/508d2bb0/attachment.html
G. Ken Holman
2010-05-19 13:43:33 UTC
Permalink
Post by Pragya Kapoor
let $entryid="1"
let $entry1:="1"
let $entry2:="2"
for $entry in doc("uri")//root/entry
return
if (empty($entry))
then
$entry1
else
$entry2
Now if there is a entry with entryid=1 then it gives $entry2=2 .
whereas if no entry exists with entryid=1 then it gives "no
results". Please suggest me some other way to check the if condition
so that I can get the desired result.
You don't say what your desired result is, but I'm guessing you want
$entry2 if there are no entries as desired:

let $entryid:="1"
let $entry1:="1"
let $entry2:="2"
return if (empty(doc("uri")//root/entry[@id=$entryid]))
then
$entry1
else
$entry2


I hope this helps.

. . . . . . . . . Ken

--
XSLT/XQuery training: after http://XMLPrague.cz 2011-03-28/04-01
Vote for your XML training: http://www.CraneSoftwrights.com/q/i/
Crane Softwrights Ltd. http://www.CraneSoftwrights.com/q/
G. Ken Holman mailto:gkholman at CraneSoftwrights.com
Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/q/bc
Legal business disclaimers: http://www.CraneSoftwrights.com/legal
sai shanker
2010-05-19 14:18:49 UTC
Permalink
Hi,
My question is do you really need empty function?
can 't you say if ($entry) then .... else ....
Thanks and Regards,
Sai.

--- On Wed, 5/19/10, G. Ken Holman <gkholman at CraneSoftwrights.com> wrote:


From: G. Ken Holman <gkholman at CraneSoftwrights.com>
Subject: Re: [MarkLogic Dev General] query on If else
To: "general at developer.marklogic.com" <general at developer.marklogic.com>
Date: Wednesday, May 19, 2010, 9:43 AM
Post by Pragya Kapoor
I have a query regarding the if-else statement in query.the code
let $entryid="1"
let $entry1:="1"
let $entry2:="2"
for $entry in doc("uri")//root/entry
return
if (empty($entry))
then
? $entry1
else
$entry2
Now if there is a entry with entryid=1 then it gives $entry2=2 .
whereas if no entry exists with entryid=1 then it gives "no
results". Please suggest me some other way to check the if condition
so that I can get the desired result.
You don't say what your desired result is, but I'm guessing you want
$entry2 if there are no entries as desired:

let $entryid:="1"
let $entry1:="1"
let $entry2:="2"
return if (empty(doc("uri")//root/entry[@id=$entryid]))
then
? $entry1
else
? $entry2


I hope this helps.

. . . . . . . . . Ken

--
XSLT/XQuery training:???after http://XMLPrague.cz 2011-03-28/04-01
Vote for your XML training:???http://www.CraneSoftwrights.com/q/i/
Crane Softwrights Ltd.? ? ? ? ? http://www.CraneSoftwrights.com/q/
G. Ken Holman? ? ? ? ? ? ? ???mailto:gkholman at CraneSoftwrights.com
Male Cancer Awareness Nov'07? http://www.CraneSoftwrights.com/q/bc
Legal business disclaimers:? http://www.CraneSoftwrights.com/legal

_______________________________________________
General mailing list
General at developer.marklogic.com
http://developer.marklogic.com/mailman/listinfo/general




-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://developer.marklogic.com/pipermail/general/attachments/20100519/8802c3ca/attachment-0001.html
Markus Pilman
2010-05-19 14:24:11 UTC
Permalink
Hi,

No, you don't need the empty function. if ($netry) then ... else ... works.
In a lot of cases, you can do it even more elegant. The following fragments are equivalent:

if ($entry1) then $entry1 else $entry2 <=> ($entry1, $entry2)[1]

Best Markus
Post by sai shanker
Hi,
My question is do you really need empty function?
can 't you say if ($entry) then .... else ....
Thanks and Regards,
Sai.
From: G. Ken Holman <gkholman at CraneSoftwrights.com>
Subject: Re: [MarkLogic Dev General] query on If else
To: "general at developer.marklogic.com" <general at developer.marklogic.com>
Date: Wednesday, May 19, 2010, 9:43 AM
Post by Pragya Kapoor
I have a query regarding the if-else statement in query.the code
let $entryid="1"
let $entry1:="1"
let $entry2:="2"
for $entry in doc("uri")//root/entry
return
if (empty($entry))
then
$entry1
else
$entry2
Now if there is a entry with entryid=1 then it gives $entry2=2 .
whereas if no entry exists with entryid=1 then it gives "no
results". Please suggest me some other way to check the if condition
so that I can get the desired result.
You don't say what your desired result is, but I'm guessing you want
let $entryid:="1"
let $entry1:="1"
let $entry2:="2"
then
$entry1
else
$entry2
I hope this helps.
. . . . . . . . . Ken
--
XSLT/XQuery training: after http://XMLPrague.cz 2011-03-28/04-01
Vote for your XML training: http://www.CraneSoftwrights.com/q/i/
Crane Softwrights Ltd. http://www.CraneSoftwrights.com/q/
G. Ken Holman mailto:gkholman at CraneSoftwrights.com
Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/q/bc
Legal business disclaimers: http://www.CraneSoftwrights.com/legal
_______________________________________________
General mailing list
General at developer.marklogic.com
http://developer.marklogic.com/mailman/listinfo/general
_______________________________________________
General mailing list
General at developer.marklogic.com
http://developer.marklogic.com/mailman/listinfo/general
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://developer.marklogic.com/pipermail/general/attachments/20100519/dba30f65/attachment.html
G. Ken Holman
2010-05-19 14:25:00 UTC
Permalink
Post by sai shanker
My question is do you really need empty function?
You could use the not() function instead:

let $entryid:="1"
let $entry1:="1"
let $entry2:="2"
return if (not(doc("uri")//root/entry[@id=$entryid]))
then
$entry1
else
$entry2
Post by sai shanker
can 't you say if ($entry) then .... else ....
Not the way you had it, because the way you had it the return clause
is conditional on there being a $entry ... it will *never* be empty
(which is what David was trying to tell you):

let $entryid="1"
let $entry1:="1"
let $entry2:="2"

for $entry in doc("uri")//root/entry
where $entry/@id = $entryid
return
if (empty($entry))
then
$entry1
else
$entry2


I think what you are missing is that the FLWOR expression creates a
set of tuples and the "return" clause acts only on the tuples
created. If you create no tuples, then the "return" clause is not
invoked. Your "if ($entry)" attempt is looking at each tuple created
by the expression ... it isn't looking at the set of tuples.

The way I wrote my expression, the "if" is checking the set of items
being addressed.

The way you wrote your expression, the "if" is checking each tuple
member (and when there are no tuples, the "if" isn't even being invoked).

Does this help you understand the situation?

. . . . . . . . . . Ken

--
XSLT/XQuery training: after http://XMLPrague.cz 2011-03-28/04-01
Vote for your XML training: http://www.CraneSoftwrights.com/q/i/
Crane Softwrights Ltd. http://www.CraneSoftwrights.com/q/
G. Ken Holman mailto:gkholman at CraneSoftwrights.com
Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/q/bc
Legal business disclaimers: http://www.CraneSoftwrights.com/legal
Loading...