Skip to content Skip to sidebar Skip to footer

Is This A Valid Xpath Query?

//[dataOptions[id]] seems to work But //[dataOptions[id=value]] doesn't work I am trying to get all the parents of dataOptions where dataOptions.id equals val. Is the second one va

Solution 1:

//[dataOptions[id]] seems to work

But //[dataOptions[id=value]] doesn't work

Both of these are with illegal syntax (the predicate must be preceded by a node - test).

I am trying to get all the parents of dataOptions where dataOptions.id equals val.

Use:

//*[dataOptions[id=val]]

this selects all elements that have a child element dataOptions whose id and val children have the same sting value.

or use if val isn't an element name but a literal string, then use:

//*[dataOptions[id='val']]

UPDATE: The OP has edited the question 3 hrs after initially asked -- now we see that the input is JSON. XPath doesn't know about JSON -- it only operates on XML documents. Therefore, this question shouldn't be tagged as XPath.

Solution 2:

ShaggyInjun, I think you're looking something in lines with:

//dataOptions[id]
//dataOptions[id="id2"]

I have recently written a js-lib called "defiant.js" - with which one can make queries on JSON structure with XPath. To put in in this context - I've pasted your JSON data at this page:

http://www.defiantjs.com/#xpath_evaluator

...and visually tested different XPath queries agains the structure. The evaluator highlights the matches both in JSON and its XML counterpart.

I hope you find the "defiant.js" and its site usefull.

Solution 3:

Is there a place I can view the JSON data you're trying to use JPath against?

You can also try using the chained method of querying out your data.

var jp = newPath(JSONDATA);

jp.$('dataOptions').$(function(n){
    return( n.$('id').json == yourvalue );
}).json;

The XPath portion of JPath simply uses some regexp's to create code like above.

Post a Comment for "Is This A Valid Xpath Query?"