S
S
Shimpanze2019-06-29 15:22:47
XPath
Shimpanze, 2019-06-29 15:22:47

How in xPath, when searching, do not take into account empty nodes?

Hello!
It is necessary to find all images that are the only elements of their parent - links.
Should find:

<a href="#"><img src="image.jpg" /></a>

<a href="#">    <img src="image.jpg" />    </a>

<a href="#">
  <img src="image.jpg" />
</a>

Should not find:
<a href="#">
  <img src="image.jpg" />
  <img src="image.jpg" />
</a>

<a href="#">
  <img src="image.jpg" />
  Изображение с изображением изображения
</a>

<a href="#"><img src="image.jpg" />Изображение с изображением изображения</a>

Here is the condition:
//a[count(node())=1][img]
...coincides only with the first example. Since (something tells me) it takes spaces as a text node.
It seems like two functions should be used here: normalize-space()and concat(). I've been struggling all day and nothing works.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Fov, 2019-06-30
@Shimpanze

Everything is simple if you break it into parts
1. Let's look at the text content of all links and cut off those that are not empty.
2. Count the children and leave only elements with one child
3. Done
XML

<root>
  <a href="#"><img src="image1.jpg" /></a>
  <a href="#">    <img src="image2.jpg" />    </a>
  <a href="#">
    <img src="image3.jpg" />
  </a>
  <a href="#">
    <img src="image4.jpg" />
    <img src="image5.jpg" />
  </a>
  <a href="#">
    <img src="image6.jpg" />
    Изображение с изображением изображения
  </a>
  <a href="#"><img src="image7.jpg" />Изображение с изображением изображения</a>
</root>

XPath
result
Element='<img src="image1.jpg"/>'
Element='<img src="image2.jpg"/>'
Element='<img src="image3.jpg"/>'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question