[MarkLogic Dev General] xpath string construction

Eric Palmitesta eric.palmitesta at utoronto.ca
Tue Oct 14 09:04:51 PDT 2008


Seems like a race condition to me, Geert.  Maybe xdmp:lock-release() and 
xdmp:lock-acquire can help with that though.

Eric

Geert Josten wrote:
> Hi Eric,
> 
> If you are willing to add identifying attributes anyway, why not use simple integer based values, allowing use of expressions like max(//@id) + 1 to calculate the next id value? Or if you need to calculate multiple values in one pass: max(//@id) + count(preceding-sibling::*) + 1..
> 
> Kind regards,
> Geert
> 
> 
> 
> Drs. G.P.H. Josten
> Consultant
> 
> 
> http://www.daidalos.nl/
> Daidalos BV
> Source of Innovation
> Hoekeindsehof 1-4
> 2665 JZ Bleiswijk
> Tel.: +31 (0) 10 850 1200
> Fax: +31 (0) 10 850 1199
> http://www.daidalos.nl/
> KvK 27164984
> De informatie - verzonden in of met dit emailbericht - is afkomstig van Daidalos BV en is uitsluitend bestemd voor de geadresseerde. Indien u dit bericht onbedoeld hebt ontvangen, verzoeken wij u het te verwijderen. Aan dit bericht kunnen geen rechten worden ontleend.
> 
> 
>> From: general-bounces at developer.marklogic.com
>> [mailto:general-bounces at developer.marklogic.com] On Behalf Of
>> Eric Palmitesta
>> Sent: dinsdag 14 oktober 2008 16:48
>> To: General Mark Logic Developer Discussion
>> Subject: Re: [MarkLogic Dev General] xpath string construction
>>
>> Hi Wayne,
>>
>> Yes, I've been looking into generating unique identifiers to
>> ease such things as deletion.  I'm still new to the
>> 'document' model, still figuring out what's portable from my
>> 'relational' model experience.
>>
>> Is xdmp:request() guaranteed to be unique?  If so, that's a
>> candidate to use as a unique identifier when inserting a new node.
>>
>> If there's a way to synchronize a particular block of code
>> across all sessions across all e-nodes, a hash of
>> xdmp:request-timestamp() might also work.
>>
>> I'm sure some mailing-list-folk have needed to generate an
>> identifier which is guaranteed unique, anyone have
>> suggestions / advice?
>>
>> Much thanks,
>>
>> Eric
>>
>> Wayne Feick wrote:
>>> Hi Eric,
>>>
>>> In 4.0, you can use xdmp:unpath() to do this
>>>
>>>
>>>
>> http://developer.marklogic.com/pubs/4.0/apidocs/Extension.html#xdmp:un
>>> path
>>>
>>> However, in the example you've given I'd recommend changing the
>>> approach to use some sort of an id attribute on person (since there
>>> are duplicate
>>> names) rather than a positional XPath expression. With your current
>>> approach, two users could each intend to delete "bob" at
>> index 3 when
>>> in fact the second attempt would actually delete "ryan".
>>>
>>> As a rule, exposing xpath expressions to a web app is
>> dangerous since
>>> there is no guarantee they still refer to the same node from one
>>> transaction to the next.
>>>
>>> Wayne.
>>>
>>>
>>> On Fri, 2008-10-10 at 14:43 -0400, Eric Palmitesta wrote:
>>>> Is there a specific reason why one can't construct an
>> xpath out of a string?
>>>> For example,
>>>>
>>>> let $media := 'book' (: or 'journal', or 'article' :) return
>>>>    doc('/path/to/file.xml')/path/to/$media/title
>>>>
>>>> Another use case, I want to display a list of items, and offer a
>>>> 'delete' link for each item.
>>>>
>>>> lets say /people.xml contained the following:
>>>>    <people>
>>>>      <person name="bob" />
>>>>      <person name="jim" />
>>>>      <person name="bob" />
>>>>      <person name="ryan" />
>>>>    </people>
>>>>
>>>> So I'd display something like:
>>>>
>>>> for $person in doc('/people.xml')/people/person return
>>>>    <div>
>>>>      $person/@name
>>>>      <a href="delete.xqy?path={ xdmp:path($person) }>delete</a>
>>>>    </div>
>>>>
>>>> This will give me nice delete links like
>>>> "delete.xqy?path=/people/person[1]", but in the supposed
>> delete.xqy,
>>>> I'd want to do something similar to:
>>>>
>>>> let $file := '/people.xml'
>>>> let $person := xdmp:get-request-field('path') return
>>>>    xdmp:node-delete(doc($file)/$person)
>>>>
>>>> I can't, of course, the doc call will be fine but I can't
>> construct
>>>> xpath with a string.  And the node-delete (and any other
>>>> node-manipulation function) requires actual nodes, not strings.
>>>>
>>>> I end up having to write eval-based utility functions:
>>>>
>>>> define function util:remove-element($uri as xs:string, $xpath as
>>>> xs:string) {
>>>>      let $node := concat("doc('", $uri, "')", $xpath)
>>>>      return
>>>>              xdmp:eval(concat("xdmp:node-delete(", $node, ")")) }
>>>>
>>>> Please tell me I'm all wrong and there's a better way.
>>>>
>>>> Cheers,
>>>>
>>>> Eric
>>>> _______________________________________________
>>>> General mailing list
>>>> General at developer.marklogic.com
>>>> <mailto:General at developer.marklogic.com>
>>>> http://xqzone.com/mailman/listinfo/general
>>>
>> ----------------------------------------------------------------------
>>> --
>>>
>>> _______________________________________________
>>> General mailing list
>>> General at developer.marklogic.com
>>> http://xqzone.com/mailman/listinfo/general
>> _______________________________________________
>> General mailing list
>> General at developer.marklogic.com
>> http://xqzone.com/mailman/listinfo/general
>>
> 
> _______________________________________________
> General mailing list
> General at developer.marklogic.com
> http://xqzone.com/mailman/listinfo/general


More information about the General mailing list