Search code examples
marklogic

How to do xpath of existing xpath variable?


I am trying to use xpath on already xpath element using javascript query. Below code giving error

[javascript]

JS-JAVASCRIPT: b.xpath("/cart") -- Error running JavaScript request: TypeError: undefined is not a function.

var a = cts.doc("/users/mydoc.xml");

var b = a.xpath("/user/carts");

b.xpath("/cart");

Solution

  • The xpath function in Server-side JavaScript returns a Sequence. Either iterate over that, or if you expect only one result, apply fn.head on it. Also make sure to use relative XPaths if continuing from a descendant. Prefix your path with ., or just use relative paths:

    var a = cts.doc("/users/mydoc.xml");
    
    var b = fn.head(a.xpath("/user/carts"));
    
    b.xpath("cart");
    

    HTH!