Exercise

Get to know the position() function

As you saw in the video, the position() function is very powerful when used within a predicate. Together with operators, you can basically select any node from those that match a certain path.

You'll try this out with the following HTML excerpt that is available to you via rules_html. Let's assume this is a continuously updated website that displays certain Coronavirus rules for a given day and the day after.

...
<div>
  <h2>Today's rules</h2>
  <p>Wear a mask</p>
  <p>Wash your hands</p>
</div>
<div>
  <h2>Tomorrow's rules</h2>
  <p>Wear a mask</p>
  <p>Wash your hands</p>
  <small>Bring hand sanitizer with you</small>
</div>
...

Instructions 1/3

undefined XP
  • 1
    • For starters, extract the text of the second p in every div using XPATH.
  • 2
    • Now extract the text of every p (except the second) in every div.
  • 3
    • Extract the text of the last three children of the second div.
    • Use the >= operator for selecting these children nodes.