[MarkLogic Dev General] If xml element exists

Erik Hennum Erik.Hennum at marklogic.com
Thu Jan 28 06:33:42 PST 2016


Hi, Pragya:

I'd like to make a different point:

*  If you know exactly which document contains the information and need to inspect that document, use fn:doc() to read the document.

* If you're not sure which document contains the information, use search (whether cts:search() or the JSearch or Search APIs) to match the document that contains the information.

To put it the other way, avoid reading a large set of candidate documents with fn:doc and then checking each candidate document with XPaths to determine whether the candidate contains the information.

(The equivalent to that antipattern in a relational database would be using a stored procedure to read and filter a large number of candidate rows instead of using an SQL WHERE clause to read only the appropriate rows.)

Either way, after you have retrieved the correct document, the suggestions from the previous note apply to inspecting the document with XPaths.


Hoping that clarifies,


Erik Hennum
________________________________
From: general-bounces at developer.marklogic.com [general-bounces at developer.marklogic.com] on behalf of Kapoor, Pragya [PKapoor1 at innodata.com]
Sent: Wednesday, January 27, 2016 9:52 PM
To: MarkLogic Developer Discussion
Subject: Re: [MarkLogic Dev General] If xml element exists



Erik,


If I understand correctly, you mean to say, if I know the document from where I need to fetch the elements, still the cts:search will be faster than fn:doc()


For example: fn:doc("/misc/SavedTextSearches.xml")/TextSearches/User[Name = $User] )  (: slower :)

                        cts:search(/TextSearches/User, cts:element-value-query(xs:QName("Name"), $User)  (: faster:)

Thanks
Pragya
________________________________
From: general-bounces at developer.marklogic.com <general-bounces at developer.marklogic.com> on behalf of Erik Hennum <Erik.Hennum at marklogic.com>
Sent: Wednesday, January 27, 2016 7:13 PM
To: MarkLogic Developer Discussion
Subject: Re: [MarkLogic Dev General] If xml element exists

Hi, Pragya:

If you need to retrieve the document anyway, you can test for the existence or absence of nodes with:

    http://docs.marklogic.com/fn:exists
    http://docs.marklogic.com/fn:empty

as in

    let $root:= fn:doc("/misc/SavedTextSearches.xml")/TextSearches
    let $hasUser := ($root/User/Name/string(.) = $User)
    let $isHidden := fn:exists($root/Hidden)
    return ...

To find documents of interest based on criteria, however, you should use cts:search() instead of fn:doc(),

fn:doc() retrieves documents into memory before inspecting the document, where cts:search() matches against the indexes and thus (depending on the criteria) retrieves many fewer documents much more quickly.


Hoping that helps,


Erik Hennum
________________________________
From: general-bounces at developer.marklogic.com [general-bounces at developer.marklogic.com] on behalf of Kapoor, Pragya [PKapoor1 at innodata.com]
Sent: Tuesday, January 26, 2016 10:32 PM
To: MarkLogic Developer Discussion
Subject: Re: [MarkLogic Dev General] If xml element exists



Few more examples:


 if ($DocRecord[fn:not(.[Hidden])])
                then
                 ()
               else
                    $DocRecord


if ($NumOfGroups = 1)
        then
          $DocRecords[fn:not(.[Hidden])]
        else ()


________________________________
From: general-bounces at developer.marklogic.com <general-bounces at developer.marklogic.com> on behalf of Kapoor, Pragya <PKapoor1 at innodata.com>
Sent: Wednesday, January 27, 2016 11:38 AM
To: MarkLogic Developer Discussion
Subject: [MarkLogic Dev General] If xml element exists



Hi,


I need to check the below conditions at multiple places, if there are other more performant way to achieve these:


if ( fn:doc("/misc/SavedTextSearches.xml")/TextSearches/User[Name = $User] )
  then
    "YES"
  else
    "NO"


if ( fn:doc("/misc/SavedTextSearches.xml")/TextSearches )
  then
    ( )
  else 'p'

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."
"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."
"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/20160128/2f954847/attachment-0001.html 


More information about the General mailing list