[Haskell-cafe] Backtracking in HXT
Mateusz Kowalczyk
fuuzetsu at fuuzetsu.co.uk
Tue Jan 1 14:25:11 CET 2013
Ah, zeroArrow looks exactly like what I need here!
Thanks, this helps.
Mateusz Kowalczyk
On 01/01/13 01:08, dag.odenhall at gmail.com wrote:
> (">>>" is my GHCi prompt, might be a bit confusing for an arrow example.)
>
>
> On Tue, Jan 1, 2013 at 2:05 AM, dag.odenhall at gmail.com
> <mailto:dag.odenhall at gmail.com> <dag.odenhall at gmail.com
> <mailto:dag.odenhall at gmail.com>> wrote:
>
> 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
> <http://example.com/somelink.html>'>Hello One!</a><a
> href='example.com/anotherlink.html
> <http://example.com/anotherlink.html>'>Hello Two!</a></html>" >>>
> deep getA
> ["example.com/anotherlink.html <http://example.com/anotherlink.html>"]
>
>
>
> On Mon, Dec 31, 2012 at 3:36 PM, Mateusz Kowalczyk
> <fuuzetsu at fuuzetsu.co.uk <mailto: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 <mailto:Haskell-Cafe at haskell.org>
> 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/113e121f/attachment.htm>
More information about the Haskell-Cafe
mailing list