4

I want to select sibling text node of an element. This is how I select it in XPath

//div[@class='someclass']/following-sibling::node()

Above code would select text SALAAM in XPath

<foo>
  <div class='someclass'/>
  SALAAM
  <bar>something</bar>
</foo>

Is this possible with JSoup selectors? I don't want to code java, just need some sizzle-like selector.

Please note that this is is different from Getting text of element without including text of sub-element with jsoup, because the answer there is not pure selector expression.

Community
  • 1
  • 1
Mohsen
  • 3,512
  • 3
  • 38
  • 66
  • 1
    Yes I tried, no avail. CSS don't consider text nodes as a real node, so no way to specify it in CSS. When you say ''siblingA + siblingB'', what do you put for siblingB when it's actually a text node not an html tag element? – Mohsen Apr 06 '15 at 09:16
  • 7
    The JSoup selectors do not support selecting text nodes. So, selecting the text node's container is as close as you can get. – Tomalak Apr 06 '15 at 09:16
  • Added some description. Thanks @Tomalak. I believe jsoup selectors do not select text nodes. – Mohsen Apr 06 '15 at 09:29
  • Selecting the parent or an adjacent element sibling and then using the DOM API to grab the interesting node should not be too difficult, though. – Tomalak Apr 06 '15 at 09:31
  • 1
    Yes I know. But here I cannot code java. My app just accepts selectors. – Mohsen Apr 06 '15 at 09:36
  • 1
    @BoltClock. I think that this is not a duplicate, since OP asking how to do this using a CSS selector, and not how to do it in code. – Jonas Czech Apr 07 '15 at 07:46
  • 1
    Fair enough. The answer will most likely point to the solution there however, as it is simply not possible to do it without code. – BoltClock Apr 07 '15 at 07:48
  • 1
    You could [convert your Jsoup Document to an org.w3c.dom.Document](http://stackoverflow.com/questions/17802445/unable-to-convert-jsoup-document-to-w3c-document) and then use XPath on that. – Jonas Czech Apr 09 '15 at 17:07
  • Only a small correction—your XPath selector selects SALAAM but the `` sibling as well (it selects all following siblings). You could use `//div[@class='someclass']/following-sibling::node()[1]` or `//div[@class='someclass']/following-sibling::text()`. – Honza Hejzl Apr 05 '16 at 07:38

0 Answers0