Function mapping can actually be very handy, you
shouldn't just automatically turn it off and forget
about it.

   The call-zero-times-on-empty-sequence behavior can
be useful to, for example, only invoke processing when
something actually exists.  Eliminating the need to sprinkle
if/then/else statements through your code.  For example:

function transform-order ($order as element(order)?)
{
    <expanded-order>{
        $order/cust-id, $order/item-code, lookup-coupon-code 
($order/coupon-code)
    }</expanded-order>
}

function lookup-coupon-code ($code as element(coupon-code))
{
    <coupon-code db-id="{some-lookup-function 
($code)}">{$code/node()}</coupon-code>
}

   Given this input:

<order>
   <cust-id>1234</cust-id>
   <item-code>5678</item-code>
   <coupon-code>abcd</coupon-code>
   <coupon-code>wxyz</coupon-code>
</order>

   You get (lookup-coupon-code was invoked twice, once for each element):

<order>
   <cust-id>1234</cust-id>
   <item-code>5678</item-code>
   <coupon-code db-id="3242343243">abcd</coupon-code>
   <coupon-code db-id="9583475485">wxyz</coupon-code>
</order>

   With this input:

<order>
   <cust-id>1234</cust-id>
   <item-code>5678</item-code>
</order>

   You get (lookup-coupon-code was never called):

<order>
   <cust-id>1234</cust-id>
   <item-code>5678</item-code>
</order>

   No conditional statement needed to avoid calling lookup-coupon-code,
and no loop needed to iterate over n coupon codes when present.

   Simple, powerful code, but you need to understand the subtleties.

   An alternate way to get similar behavior is to use function
steps in XPath expressions, like this:

function transform-order ($order as element(order)?)
{
    <expanded-order>{
        $order/cust-id, $order/item-code, $order/coupon-code/lookup-coupon-code 
(.)
    }</expanded-order>
}

   This works whether function mapping is enabled or not (it's
part of the XPath spec). But beware, your function may not be
called in the same order that the nodes occur in the document.
If order is important, use a FLWOR or rely on function mapping.

   More information than you probably care about, but now you know.

On Jan 10, 2012, at 11:33 AM, Hans-Juergen Rennau wrote:

> Hi Geert und Ron,
> 
> thanks a lot for your explanations! I will definitely make it a habit, as you 
> suggest, Geert, to specify in all modules "xdmp:mapping 'false'". 
> 
> Hans-Juergen
> 
> Von: Geert Josten <geert.jos...@dayon.nl>
> An: General MarkLogic Developer Discussion <general@developer.marklogic.com>; 
> Hans-Juergen Rennau <hren...@yahoo.de> 
> Gesendet: 8:12 Dienstag, 10.Januar 2012
> Betreff: RE: [MarkLogic Dev General] sequence type matching - bug (?)
> 
> Hi Hans,
> 
> It is one of the side-effects of running your XQuery code within MarkLogic
> Server with version indication '1.0-ml' (which is probably the default in
> your App Server if you omitted the XQuery Version Declaration). Running
> your code as '1.0' will also behave as expected, but you would miss
> goodies like try/catch. Make it a custom to add the xdmp:mapping false
> option to all your code.
> 
> Kind regards,
> Geert
> 
> _______________________________________________
> General mailing list
> General@developer.marklogic.com
> http://developer.marklogic.com/mailman/listinfo/general

---
Ron Hitchens {mailto:r...@ronsoft.com}   Ronsoft Technologies
     +44 7879 358 212 (voice)          http://www.ronsoft.com
     +1 707 924 3878 (fax)              Bit Twiddling At Its Finest
"No amount of belief establishes any fact." -Unknown




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

Reply via email to