Swagata Mondal
Swagata Mondal

Reputation: 53

how to implement when in xpath where i want to validate a parent node is same as present in condition?

       "name" : "a",
       "type": "container",
       "kids": [{
         "name": "a1",
         "type": "container",
         "kids": [{
           "name": "a2",
           "type": "leaf",
           "when": "../a"
         }]
       }]
     }

in when condition i want to validate in node ->parent ->parent is "a" Now how to validate that condition

Upvotes: 1

Views: 67

Answers (3)

Martin Honnen
Martin Honnen

Reputation: 167401

XPath 3.1 can query JSON as XDM maps/arrays, the lookup operator is ? so for your sample (a bit formatted)

{
  "name" : "a",
  "type": "container",
  "kids": [
    {
     "name": "a1",
     "type": "container",
     "kids": [
       {
         "name": "a2",
         "type": "leaf",
         "when": "../a"
       }
    ]
    }
  ]
}

you can test e.g.

?name = 'a' and ?kids?*?kids?*?when = '../a'

and will give true. I am not sure, however, whether you expect the node XPath / step syntax to be used, that is not what XPath 3.1 offers; you might find it in some other implementations that internally convert the JSON to some XML.

Online XPath 3.1 fiddle.

Upvotes: 0

Michael Kay
Michael Kay

Reputation: 163262

To process JSON using XPath you need an XPath 3.1 processor.

The tree model for JSON, unlike that for XML, does not have parent pointers, which means you can't navigate from a node to its parent. This basically means that you have to gather the information you need as you descend the tree, and pass it down in the form of parameters.

Fleshing this out needs a bit more context to understand exactly what you are trying to do.

Upvotes: 0

Siebe Jongebloed
Siebe Jongebloed

Reputation: 4834

If your content is XML and you want to check if the parent of the parent of the context is a a, you can use this:

parent::*/parent::a

Upvotes: 0

Related Questions