[Haskell-cafe] Predicates in HaXml

Malcolm Wallace Malcolm.Wallace at cs.york.ac.uk
Wed Nov 10 08:53:57 EST 2004

Tom Spencer <t.e.spencer at gmail.com> writes:

> In XSLT there is an XPath function that will let you select a
> particular node in the current context, for example;
> <xsl:value-of select="team[1] "/> 
> This selects the first team element in the current context. Is there a
> work around to get similar functionality from the HaXml library?

It's easy to define, yes.  Here is an "indexing" combinator that
chooses one result of a CFilter, based on numeric position:

    index :: Int -> CFilter -> CFilter
    index n f = (\c-> [ (f c)!!n ])

So for your example, you want to write something like:

    index 0 (tag "team") `o` children

This is probably the simplest way of doing your specific task, but
there are of course other ways.  The tool Xtract has a parser for
XPath-like expressions, including more complex predicates, which get
translated directly to combinator-style accessors.  Unfortunately,
the set of combinators used in Xtract is slightly different from
those in the rest of HaXml, but they bear a close relationship.

You could also use LabelFilters, e.g.

    (\cs-> ... cs!!0 ...) `oo` numbered (tag "team" `o` children)

and doubtless there are other techniques too.


More information about the Haskell-Cafe mailing list