Hi Tim,

I think I understand why you put all that $action and if logic inside the 
function, but I also think your problem could be less complex if you didn't.

Next to this, you are checking for changes for each value individually. But how 
about building your XML first, and simply doing a deep-equal to compare it with 
the database doc? If your form-values cover most of your XML, and the XML isn't 
very big, that could work equally fast.

You don't need the db-values by the way if $action is 'New', so you could save 
yourself the effort looking for them in that case. But only if you put the if 
logic outside the function. ;-) (or the logic to retrieve them inside the 
function..)

HTH!

Kind regards,
Geert

-----Oorspronkelijk bericht-----
Van: [email protected] 
[mailto:[email protected]] Namens Tim Meagher
Verzonden: donderdag 15 september 2011 17:33
Aan: 'General MarkLogic Developer Discussion'
Onderwerp: Re: [MarkLogic Dev General] How to effectively pass variables by 
reference to an xquery function?

Hi Geert,

It makes sense, but I'd like to call just one function that receives
multiple parameters:

- existing database value
- request field name
- action
- new value

When invoked, the function uses the action to determine what value should be
assigned to a local variable and sets a status if the values differ.  So
rewriting my example we have:

xquery version "1.0-ml";

declare namespace t = "test";
declare variable $t:changed := false();

declare function t:working-value($action, $db-value, $request-field,
$new-value)
{
    let $form-value := 
        if ($action eq ("Get", "Save")) then
xdmp:get-request-field($request-field) else ()
    return
        if ($action eq "New") then $new-value
        else if ($action eq "Retrieve") then (
            $db-value,
            if (not($t:changed) and $db-value ne $form-value) then
xdmp:set($t:changed, true()) else ()           
        )
        else if ($action eq "Save") then (
            $form-value,
            if (not($t:changed) and $db-value ne $form-value) then
xdmp:set($t:changed, true()) else ()
        )
};

let $name := t:working-value("Save", "Joe", "Name", "")
let $position := t:working-value("Save", "Joe", "Name", "")
(: let $local-var := t:working-value(...) :)

return (
    if ($save) 
        then (: build XML document using name, position, ... 
                and insert it into the database :) 
        else (),
    (: populate form :)
)

Cool?

Tim

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Geert Josten
Sent: Thursday, September 15, 2011 10:54 AM
To: General MarkLogic Developer Discussion
Subject: Re: [MarkLogic Dev General] How to effectively pass variables by
reference to an xquery function?

Hi Tim,

Ah. In that case I would have written a 'has-changed' function, that returns
a Boolean. You apply that to all values first, and check for trues and
falses after that. You can do that more fixed, but you could also collect
Boolean in a sequence and look for a true in that as well. Makes sense?

Kind regards,
Geert



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

Reply via email to