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:unpath

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
[email protected] <mailto:[email protected]>
http://xqzone.com/mailman/listinfo/general

------------------------------------------------------------------------

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

Reply via email to