You can always try using cardinality ?.  This will allow you to pass (0,1) 
arguments to function.  Of course you have check the value with 
fn:exists($sessionId) and handle case accordingly

xutils:validateSession($sessionId as xs:string?) {..}
-----Original Message-----
From: [email protected] 
[mailto:[email protected]] On Behalf Of 
[email protected]
Sent: Friday, September 26, 2014 8:08 AM
To: [email protected]
Subject: General Digest, Vol 123, Issue 50

Send General mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://developer.marklogic.com/mailman/listinfo/general
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific than "Re: 
Contents of General digest..."


Today's Topics:

   1. Re: Passing empty value to function not   working (David Ennis)
   2. Re: Passing empty value to function not   working (David Lee)


----------------------------------------------------------------------

Message: 1
Date: Fri, 26 Sep 2014 13:14:50 +0200
From: David Ennis <[email protected]>
Subject: Re: [MarkLogic Dev General] Passing empty value to function
        not     working
To: MarkLogic Developer Discussion <[email protected]>
Message-ID:
        <cabbx6anv9uud8t9jyb5ydas5w3ywszvemd0dtyjwupcfozn...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

?Hi

MarkLogic allows for function overloading.  Perhaps that could be your 
solution..

Kind Regards,
David Ennis?



Kind Regards,
David Ennis


David Ennis
*Content Engineer*

[image: HintTech]  <http://www.hinttech.com/> Mastering the value of content 
creative | technology | content

Delftechpark 37i
2628 XJ Delft
The Netherlands
T: +31 88 268 25 00
M: +31 63 091 72 80

[image: http://www.hinttech.com] <http://www.hinttech.com> 
<https://twitter.com/HintTech>  <http://www.facebook.com/HintTech>
<http://www.linkedin.com/company/HintTech>

On 26 September 2014 13:00, Kapoor, Pragya <[email protected]> wrote:

>  Hi,
>
>
>  I need to pass an empty value to util function, which is not working.
>
>
>  declare function xutils:validateSession($sessionId as xs:string) {
>
>   if($sessionId ne "" ) then
>       let $document := fn:doc($config:USER_SESSIONS)
>      <errorCode>
>       <code>500</code>
>       <description>server error</description>
>       </errorCode>
>      else
>         <errorCode>
>       <code>517</code>
>       <description>Session Id can't be empty</description>
>       </errorCode>
>
>     };
>
>  I am calling this function from some other file and it is returing an 
> empty sequence.But if I make this function 
> local:validateSession($sessionId as xs:string)  then its working.
>
>  Calling file:
>  import module namespace config = "config"
>     at "/rest-apis/utils/config.xqy";
>
>   let $node := '<inputString>
>
>                     <sessionId></sessionId>
>                </inputString>'
>
>
>
>  let $node := xdmp:unquote($node)
>  let $sessionId := $node//sessionId/text()  ?return let $validateFlag 
> := xutils:validateSession($sessionId)
> return
>
>  $validateFlag
>
>  Please advice.
>
>  Thanks
>  Pragya
>
>
>  "This e-mail and any attachments transmitted with it are for the sole 
> use of the intended recipient(s) and may contain confidential , 
> proprietary or privileged information. If you are not the intended 
> recipient, please contact the sender by reply e-mail and destroy all 
> copies of the original message. Any unauthorized review, use, 
> disclosure, dissemination, forwarding, printing or copying of this 
> e-mail or any action taken in reliance on this e-mail is strictly prohibited 
> and may be unlawful."
>
> _______________________________________________
> General mailing list
> [email protected]
> http://developer.marklogic.com/mailman/listinfo/general
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://developer.marklogic.com/pipermail/general/attachments/20140926/2461825e/attachment-0001.html
 

------------------------------

Message: 2
Date: Fri, 26 Sep 2014 12:08:06 +0000
From: David Lee <[email protected]>
Subject: Re: [MarkLogic Dev General] Passing empty value to function
        not     working
To: MarkLogic Developer Discussion <[email protected]>
Message-ID:
        <6ad72d76c2d6f04d8be471b70d4b991e04c9b...@exchg10-be02.marklogic.com>
Content-Type: text/plain; charset="utf-8"

Almost certainly this is due to "xdmp:mapping" being on
https://docs.marklogic.com/guide/xquery/enhanced#id_55459
https://docs.marklogic.com/guide/xquery/langoverview#id_45023

For some this is an incredible feature.  For others its surprising and 
difficult to debug .
Its ON by default.
In short, what it does is call functions that have an argument that can take 
exactly 1 of something N times depending on the argument you pass so you don?t 
have to write for loops.  A side effect is that if N is 0 then the function is 
never called.
I recommend that until you understand and decide for yourself if this is 
incredibly useful or too confusing to disable it.

Add this line right under the version line in your XQuery files

declare option xdmp:update "false";



However, you do have another issue  ... you have declared your function to take 
exactly 1 argument ...
So when you turn this off, instead of not calling your function you will get an 
error.
You need to either change your function signature to allow the parameter to be 
empty, or pass in a zero length string.

It looks like your function is expecting "" instead of () -- which means that 
your XPath is finding no text nodes let $sessionId := $node//sessionId/text()


This use of text() is generally discouraged.   Unless you absolutely positively 
know you need to use text(),  don?t.  Just don?t do it.
Instead use string()
let $sessionId :=   fn:string($node//sessionId)

( you can scan the archives of the various XQuery forums for a decade long 
debate on this if your curious)



Now this will give you an error if there is > 1 node with a seesionID element 
...  but your code is not setup to handle N nodes so that?s probably for the 
best.






From: [email protected] 
[mailto:[email protected]] On Behalf Of Kapoor, Pragya
Sent: Friday, September 26, 2014 7:01 AM
To: [email protected]
Subject: [MarkLogic Dev General] Passing empty value to function not working


Hi,



I need to pass an empty value to util function, which is not working.


declare function xutils:validateSession($sessionId as xs:string) {

if($sessionId ne "" ) then
      let $document := fn:doc($config:USER_SESSIONS)
     <errorCode>
      <code>500</code>
      <description>server error</description>
      </errorCode>
    else
        <errorCode>
      <code>517</code>
      <description>Session Id can't be empty</description>
      </errorCode>

   };

I am calling this function from some other file and it is returing an empty 
sequence.But if I make this function local:validateSession($sessionId as 
xs:string)  then its working.

Calling file:
import module namespace config = "config"
    at "/rest-apis/utils/config.xqy";

 let $node := '<inputString>

                    <sessionId></sessionId>
               </inputString>'



let $node := xdmp:unquote($node)
let $sessionId := $node//sessionId/text() ?return let $validateFlag := 
xutils:validateSession($sessionId)
return

$validateFlag

Please advice.

Thanks
Pragya


"This e-mail and any attachments transmitted with it are for the sole use of 
the intended recipient(s) and may contain confidential , proprietary or 
privileged information. If you are not the intended recipient, please contact 
the sender by reply e-mail and destroy all copies of the original message. Any 
unauthorized review, use, disclosure, dissemination, forwarding, printing or 
copying of this e-mail or any action taken in reliance on this e-mail is 
strictly prohibited and may be unlawful."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://developer.marklogic.com/pipermail/general/attachments/20140926/fa3b55de/attachment.html
 

------------------------------

_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general


End of General Digest, Vol 123, Issue 50
****************************************
_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to