Following Florent - 
Could you explain what assumptions your 'expected output' is based on ?

This is a simple case where it seems you expect that 
data values (as opposed to program code) will parse the '*' multiply operation 
and evaluated.  Beyond the fact that that isn't how XQuery (or Java or 
JavaScript or most languages) actually work -- It would be quite disastrous if 
that actually happened --- imagine someone typing a "*" into a description 
field on a form and the system deciding it meant multiply ... 

My question is - presuming you want this behavior - what syntax or language are 
you attempting to simulate ? 

This element has an interesting name and value
<functions type="decimal">CollectionNo*3</functions>

Read 'functions'  I am let to think that there may be a wide variety of syntax, 
features, maybe 'function calls' etc allowed within this element ...
Also seeing the word "CollectionNo" --- That implies that a concept of 
Variables is also expected - in this case a 'Column Alias' ... 

This looks a lot like a dump of a spreadsheet or similar product.   
 
It is certainly possible ( and not that difficult) to write parsers in XQuery 
that can do this kind of operation - 
But the key is recognizing that the 'Function Evaluator' is functionality that 
needs to be expressed explicitly and a parser and evaluator written for it - 
You have a start at that - but it takes a bit more. 
You need a robust expression tokenizer and parser,  using 
fn:replace($functions, $alias .. ) is not going to work well with arbitrary 
input ... ( imagine an alias named "A" and another "AA" ...  ) 

Then there is the issue of the expected syntax and operations ... what is the 
full syntax of the expression language you want to support ? 


Here are some good links to look at what other people have done in this area to 
give you some ideas
 

http://stackoverflow.com/questions/4794101/xquery-extract-number-from-string-to-use-in-comparison

http://www.ibm.com/developerworks/library/x-javacc1.html

http://cmsmcq.com/mib/?p=1260

http://bottlecaps.de/rex/




-----------------------------------------------------------------------------
David Lee
Lead Engineer
MarkLogic Corporation
[email protected]
Phone: +1 812-482-5224
Cell:  +1 812-630-7622
www.marklogic.com

-----Original Message-----
From: [email protected] 
[mailto:[email protected]] On Behalf Of Florent Georges
Sent: Tuesday, June 16, 2015 5:33 AM
To: MarkLogic Developer Discussion
Subject: Re: [MarkLogic Dev General] xdmp:value query

   Hi,

  I haven't looked in detail (especially whether it is a good idea in this case 
to use dynamic evaluation of XQuery, or if you should rather use something 
else), but it looks like you want to evaluate the expression, not resolve a 
variable name.

  Evaluating the string literal "$exp" gives you the value of the variable.  
Evaluating $exp, means evaluating the value of the variable as an expression 
(the value of the variable is a string, "0000049*3").
So just use xdmp:value($exp), with no quote characters.

  Regards,

--
Florent Georges
http://fgeorges.org/
http://h2oconsulting.be/


On 16 June 2015 at 11:24, Kapoor, Pragya wrote:
> Hi,
>
>
> I am running the below query :
>
>
> declare namespace MET100 = 'MET100' ;
>
>
> let $output :=
> <user><userId>test1</userId><sessionId>54C76A855C23EF8AC5AFBC59C8D4C40A</sessionId>
>
> <outputs>
>
> <output><columnAlias>CollectionNo</columnAlias>
>
> <functions type="decimal">CollectionNo*3</functions></output>
>
> </outputs></user>
>
>
> let $functions := fn:data($output//functions)
>
> let $docId := '/docs/BONY_0000049_ISDA-CSA-GB95_6749.xml'
>
> let $type := ($output//functions/@type)
>
> let $doc := fn:doc($docId)
>
> let $alias := ($output//columnAlias/text())
>
> let $ns := "MET100"
>
> let $elem := "CollectionNo"
>
> let $element := fn:concat($ns,":",$elem)
>
> let $result :=($doc//*[name() = $element])/text()
>
> let $result :=
>
> if($type eq 'string') then
>
> xdmp:value(xdmp:quote(fn:replace($functions,$alias,fn:concat("'",$result,"'"))))
>
> else
>
>   let $exp := (fn:replace($functions,$alias,$result[1]))
>
>   return xdmp:value("$exp")
>
> return  $result
>
>
> actual output is : 0000049*3
>
> Expected Output is : 147
>
>
> But Since I am using  let $exp := (fn:replace($functions,$alias,$result[1]))
> which returns a string expression, which is causing problem.
>
>
> How can I solve this ?
>
>
> 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]
> Manage your subscription at:
> http://developer.marklogic.com/mailman/listinfo/general
>
_______________________________________________
General mailing list
[email protected]
Manage your subscription at: 
http://developer.marklogic.com/mailman/listinfo/general
_______________________________________________
General mailing list
[email protected]
Manage your subscription at: 
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to