2

Been lurking here for a long time, but I needed to ask this. Please excuse my noobity.

Doing some PHP/Xpath coding for some scraping, and I want to know an XPath expression to select nodes which have a parent whose sibling, somewhere in their descendant tree, contain a node with a particular text value.

Say the node is something like span[@ng="league"] and the text value somewhere in a descendant is 'SKT', I believe it should in some way include contains(text(), 'SKT'), but I'm not quite sure on the rest. TIA.

Edit:

I've tried to create a diagram of the situation here

**
|
|
+[parent]
|    |
|    |
|   [the node I want]
|     
|     
|
|
+[sibling of "parent" node seen above]
|   |
|   *
|   |
|   +---[specific text, found with previous xpath query]
|
etc**

2 Answers2

3

The following XPath will return span[@ng="league"] elements where there is at least one text node anywhere within the span that contain substring 'SKT':

//span[@ng="league" and .//text()[contains(., 'SKT')]]

If this doesn't work then you need to be more specific i.e post minimal HTML/XML example (formatted text, not image) where the XPath above doesn't return the desired output

har07
  • 88,338
  • 12
  • 84
  • 137
  • Thank you. I will give that a try. Can you point me in the direction of something to read about how to format text for diagrams like that? I tried it as a code block, but that didn't work. – Mike Donovan Jan 26 '18 at 06:32
  • The thing is, it's not in the span itself, it's in another span, located in the sibling of the parent of the span. I will try again, to post a text-diagram – Mike Donovan Jan 26 '18 at 06:34
  • Looks like you need [`preceding-sibling`](https://developer.mozilla.org/en-US/docs/Web/XPath/Axes/preceding-sibling) axis.. Probably : `//span[@ng="league" and .//text()[contains(., 'SKT')]]/preceding-sibling::span[1]` – har07 Jan 26 '18 at 06:45
  • @MikeDonovan Instead of diagram, it will be far more useful if you post simplified XML/HTML example. You can say which element exactly you want to get from that example, and we can use it test our XPath before posting an answer. – har07 Jan 26 '18 at 06:45
  • Got it. Thank you. – Mike Donovan Jan 26 '18 at 06:50
1

If your xml is something as

<parent>
  <span ng="league">The node you want </span>
</parent>
<any>
    <any2>
       <any3>SKT</any3>
   </any2>
</any>

you can use such xpath

//span[@ng="league"][../following-sibling::*[contains(., "SKT")]]
splash58
  • 26,043
  • 3
  • 22
  • 34