[MarkLogic Dev General] xpath string construction

Wayne Feick wayne.feick at marklogic.com
Fri Oct 10 12:52:06 PDT 2008


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
> General at developer.marklogic.com
> http://xqzone.com/mailman/listinfo/general
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://xqzone.marklogic.com/pipermail/general/attachments/20081010/ed46003f/attachment.html


More information about the General mailing list