Question

I can't figure out two expressions in xpath. Can someone help ?

Here they are

  1. substring-after(substring-before(//ul[@id='biblio']/li[3], ']', '['))

  2. //h2[normalize-space(string())='name']/preceding::h1[1]

Was it helpful?

Solution

Your first expression:

substring-after(substring-before(//ul[@id='biblio']/li[3], ']', '['))

First this may find all ul elements which are at (self) or a descendant of the context of your XPath. These must have an id attribute with the value 'biblio' to me matched, from there it will find the 3rd li child element(s) from the matching ul element(s). It will then perform the substring functions on the text() of the li element(s) after atmomizing them to a string.

So for example if the text of a matched li element was hello [world]. You would end up with just world as the result. As a more complete example, given the XML input:

<div>
  <ul id="biblio">
    <li>thing [one]</li>
    <li>thing [two]</li>
    <li>thing [three]</li>
  </ul>
  <ul id="biblio">
    <li>other [a]</li>
    <li>other [b]</li>
    <li>other [c]</li>
  </ul>
</div>

You would get a sequence of two strings as the result of your XPath expression which would be three and c. Note that the use of <div> in the example input is just a container and could be any element.

Your second expression:

//h2[normalize-space(string())='name']/preceding::h1[1]

First this may find all the h2 elements which are at (self) or a descendant of the context of your XPath. These must have a text() that when atmomised to a string is equal to name. From there you then select the 1st preceding h1.

So for example, given the XML input:

<div>
  <h1>title1</h1>
  <p>stuff</p>
  <h1>title2</h1>
  <p>more stuff</p>
  <h2>name</h2>
  <p>other stuff</p>
</div>

You would get the following XML output as a result of your XPath expression:

<h1>title2</h1>

Hope that helps you understand...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top