2

Hello everyone I am developing a program in Java to read the XML using XPATH for siblings nodes (elements which are same level)

I am not able to implement below XPATH expression in Java:

//*:root/*:child1[.='child_value']/following-sibling::*:child2[*:subelement]/*:subelement3/*:subelement4/following-sibling::*:anyelement

Could any one please suggest any solution

sunil
  • 50
  • 8

1 Answers1

1

The construct *:root requires XPath 2.0, which isn't supported by Xalan. The simplest solution is to use Saxon instead.

In XPath 1.0 the way to select an element by local name alone is *[local-name()='root'] which gets a bit cumbersome for an expression like this that includes 7 such selections.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • I appreciate your suggested answer Michael. But for "following-sibling::child2" how can we write in java – sunil Sep 06 '22 at 10:03
  • 1
    You mean, how can you write it in XPath 1.0? Write `following-sibling::*[local-name()='child2']`. – Michael Kay Sep 06 '22 at 13:07
  • Thank you so much Michael for this answer. Actually I was trying *[following-sibling='child2'] So I was getting error. But now I got the answer and sorry for killing your time – sunil Sep 07 '22 at 03:16
  • 1
    If you tried `following-sibling='child2'` then I worry that you're trying to learn this language by trial and error, which is going to be a painful process. You need to understand how it hangs together by doing some reading, rather than just trying to copy examples. – Michael Kay Sep 07 '22 at 07:17