xml-conduit-selectors- jQuery-style CSS selectors for xml-conduit
Safe HaskellNone



This module re-exports commonly used functionality from the Types and ToAxis modules. To parse jQuery selectors, you will also need to import JQ.

Basic usage example:

import Text.XML.Selectors
import Text.XML.Selectors.Parsers.JQ
import Text.XML as XML
import Text.XML.Cursor (Cursor, node, fromDocument)
import Data.Default
import Control.Monad (forM_)

main = do
  doc <- XML.readFile def "example.xml"
  selector <- jqString' "div.menu a[href!='#']"
  let cursors = match selector (fromDocument doc)
  forM_ cursors $ \cursor -> do
    let n = node cursor
    print n

The Selector Types

data Selector Source #

Node-level selectors and combinators


Append Selector Selector

ab (both a and b must match)

Elem Name
Attrib AttribSelector


NthChild Int
:nth-child(n); :nth-last-child(-n)
Choice [Selector]
Having Selector
Not Selector


Instances details
Show Selector Source # 
Instance details

Defined in Text.XML.Selectors.Types

Semigroup Selector Source #

The Semigroup of selectors combines selectors with Append. a <> b selects all nodes that match a and also match b. Note however that the <> operator culls redundant combinations with the Any selector, e.g. Any <> Child <> Any is just Child, not Append Any (Append Child Any).

Instance details

Defined in Text.XML.Selectors.Types

Monoid Selector Source #

The Monoid instance, just like Semigroup, combines selectors with Append; the neutral value is, of course, Any.

Instance details

Defined in Text.XML.Selectors.Types

(<||>) :: Selector -> Selector -> Selector infixl 3 Source #

An alternative semigroup of selectors, representing choice. a <||> b selects all nodes that match a and also all nodes that match b. In other words: a <||> b == Choice a b. Note however that the <||> operator culls redundant applications of Choice, e.g., a <||> b <||> c becomes Choice [a, b, c] rather than Choice [a, Choice [b, c]]. This alternative semigroup could be extended into a monoid, with the empty choice (Choice []) as the neutral value, but we were far too lazy to add that.

data AttribSelector Source #

Attribute-level selectors


AttribExists Name
AttribIs Name Text
AttribIsNot Name Text
AttribStartsWith Name Text
AttribEndsWith Name Text
AttribContains Name Text
AttribContainsWord Name Text
AttribContainsPrefix Name Text

Applying Selectors

toAxis :: Selector -> Axis Source #

Turn a Selector into an Axis.

match :: Selector -> Cursor -> [Cursor] Source #

Directly apply a Selector to a Cursor, removing duplicates. Cursors are considered duplicate iff their focus node and ancestory are the same.

Due to the knot-tying of the Cursor type, this is not perfect: we are not considering the focus node's position within its parent, so any two nodes that are exactly identical themselves and share ancestory will be considered equal. E.g., in the following XML document:


...the two <child/> nodes will be considered equal, even though they are two distinct nodes in the DOM.

Unlike toAxis, the match function prepends an implicit self-or-descendant Axis to the selector in order to mimic the behavior of actual CSS selectors.