[Haskell-cafe] Backtracking in HXT

dag.odenhall at gmail.com dag.odenhall at gmail.com
Tue Jan 1 02:05:00 CET 2013


Use arrow notation and zeroArrow, like so:

{-# LANGUAGE Arrows #-}
import Text.XML.HXT.Core
getA =
  hasName "a" >>> proc elem -> do
    text <- getText <<< getChildren -< elem
    if text == "Hello Two!"
      then getAttrValue "href" -< elem
      else zeroArrow -< ()

>>> runX $ readString  [] "<html><a href='example.com/somelink.html'>Hello
One!</a><a href='example.com/anotherlink.html'>Hello Two!</a></html>" >>>
deep getA
["example.com/anotherlink.html"]



On Mon, Dec 31, 2012 at 3:36 PM, Mateusz Kowalczyk
<fuuzetsu at fuuzetsu.co.uk>wrote:

> Hello,
> I'm not sure if this is the right place to ask this but here goes.
>
> I'm currently working with HXT and I quite like it. There's one issue I
> can't solve (at least now without some dirty, dirty hacking) and I'm
> sure it's fairly simple.
>
> Consider the following mark-up
>
> <a href="example.com/somelink.**html <http://example.com/somelink.html>">Hello
> One!</a>
> <a href="example.com/anotherlink.**html<http://example.com/anotherlink.html>">Hello
> Two!</a>
>
> Now, what I'm trying to achieve is to get the href based on the text. I
> can test for what the text is by traversing <a>s, then using the
> getChildren >>> getText arrow. What I can't figure out is how to check
> the text and then get the href as by the time I get to the text, I'll
> further down the tree!
>
> I imagine it would look something along the lines of
>  (getChildren >>> getText <?*> "Hello Two") `guards` getAttrValue
> "href". <?*> is just a random operator I made up for illustration
> purposes that works as a predicate over arrows.
>
> Is this the right approach? Is there a built-in that already does what I
> want? It
> seems like a common task...
>
> Mateusz Kowalczyk
>
> ______________________________**_________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/**mailman/listinfo/haskell-cafe<http://www.haskell.org/mailman/listinfo/haskell-cafe>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130101/ba19b5d8/attachment.htm>


More information about the Haskell-Cafe mailing list