1
       "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

5
  • You are showing json. XPath is for xml? Commented Sep 13, 2023 at 7:13
  • Are you aware of the fact that XPath is for XML (as Siebe indicated), and your example is not XML. I can guess it's mere laziness on your part–you showed some parsed interpretation of the source XML instead of the original document–but just in case it worth indicating that you cannot process JSON documents with XPath. Commented Sep 13, 2023 at 7:55
  • If you really need to process JSON, you might look at github.com/json-path/JsonPath. Also note that if that's the only check you need to implement, it's not necessary to rely on complex processing; instead, you can parse your JSON to a custom tree-like data strucure which would make the check you want to implement a no-brainer. Commented Sep 13, 2023 at 8:10
  • My code is in JSON path and I m trying to create one validator where when will check .. means parent node is 'a' present then leaf node a2 will exist ... Commented Sep 13, 2023 at 8:35
  • @SiebeJongebloed .xpath for json Commented Sep 13, 2023 at 11:40

3 Answers 3

0

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
Sign up to request clarification or add additional context in comments.

1 Comment

my content is in json can u help me with tha
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.