Mateusz Kowalczyk | 31 Dec 15:36 2012
Picon

Backtracking in HXT

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">Hello One!</a>
<a href="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
dag.odenhall@gmail.com | 1 Jan 02:05 2013
Picon

Re: Backtracking in HXT

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">Hello One!</a>
<a href="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

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
dag.odenhall@gmail.com | 1 Jan 02:08 2013
Picon

Re: Backtracking in HXT

(">>>" 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 <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'>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">Hello One!</a>
<a href="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


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Mateusz Kowalczyk | 1 Jan 14:25 2013
Picon

Re: Backtracking in HXT

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 <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'>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">Hello One!</a>
<a href="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



_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane