0

Input JSON :

[
    {"Activity":"yes","SBNumber":"123"},
    {"Activity":"yes","SB#":"123"}
]

JXPath Expressions:

//works : returns "123"
JXPathContext.newContext(input).getValue("//*/SBNumber")

//fails: throwing JXPathInvalidSyntaxException
JXPathContext.newContext(input).getValue("//*/SB#")

How do i escape # to retrieve value from SB# ?

Failed attempts include

JXPathContext.newContext(input).getValue("//*/SB\\#")  
JXPathContext.newContext(input).getValue("//*/SB[#]")
JXPathContext.newContext(input).getValue("//*/SB*")    

etc..

However, I am able to retrieve node from below

JXPathContext.newContext(input).getValue("//*/*[contains(.,'SB#')]")

But then it is reference to the parent node only :-(

Any possible solution / alternative will be helpful.. tx

prash
  • 896
  • 1
  • 11
  • 18
  • may be this? https://stackoverflow.com/questions/14457960/escaping-special-characters-inside-xpath – LifeApi Sep 26 '17 at 09:18

1 Answers1

1

Adding a context variable can help escaping like mentioned below-

JXPathContext context = JXPathContext.newContext(input);
context.getVariables().declareVariable("blabla", "SB#");
context.getValue("//*/$blabla");
Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
LifeApi
  • 56
  • 4