简体   繁体   中英

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 valid ? I have a doubt my JPath that I am using to resolve query has a bug. But I am not sure if my query is valid.

[
{
    "dataOptions": {
        "id": "id1",
        "count": "10"
    },
    "name": "Leon",
    "children": [
        {
            "dataOptions": {
                "id": "id2",
                "count": "10"
            },
            "name": "Leon",
            "children": [],
            "isExpanded": false
        },
        {
            "dataOptions": {
                "id": "id2",
                "count": "10"
            },
            "name": "Leon",
            "children": [],
            "isExpanded": false
        }
    ],
    "isExpanded": false
},
{
    "dataOptions": {
        "id": "id3",
        "count": "10"
    },
    "name": "Leon",
    "children": [],
    "isExpanded": false
},
{
    "dataOptions": {
        "id": "id4",
        "count": "10"
    },
    "name": "Leon",
    "children": [],
    "isExpanded": false
}

]

//[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 dataOption s 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.

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 = new Path(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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM