Select nodes matching one of several conditions (OR)

69 views Asked by At

Using Text.XML.Cursor, is there a convenient way of selecting nodes matching one of several conditions (like an or function)?

How can I get the cursors of all <p class="myclass"> and <h1> nodes (in the right order) in the following example?

<div>
    <p></p>
    <div></div>
    <h1></h1>
    <hr>
    <p class="myclass"></p>
    <h1></h1>
</div>

extract :: Cursor -> [Cursor]
-- Returns 3 cursors [h1, p, h1]
1

There are 1 answers

1
Bartek Banachewicz On

There's checkElement which accepts a predicate.

checkElement :: Boolean b => (Element -> b) -> Axis

So with that, and knowing that your extract is an Axis1:

extract :: Axis
extract = checkElement yourPredicate
  where
    yourPredicate (Element name _ _) = any (== name) ["p", "h1"]`

Adding the class check should be easy then; match the 2nd element from Element constructor, look for the class attribute, then inspect its value for myclass presence.


1 type Axis = Cursor -> [Cursor]