skip to Main Content
       "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

3

Answers


  1. 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
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search