Difference between revisions of "HXT/Practical/Simple1"

From HaskellWiki
< HXT‎ | Practical
Jump to navigation Jump to search
(use language pragma)
Line 48: Line 48:
   
 
> {-# LANGUAGE Arrows, NoMonomorphismRestriction #-}
 
> {-# LANGUAGE Arrows, NoMonomorphismRestriction #-}
> import Text.XML.HXT.Arrow
+
> import Text.XML.HXT.Core
   
 
</hask>
 
</hask>
Line 66: Line 66:
   
 
<hask>
 
<hask>
Main> runX (readDocument [(a_validate,v_0)] "simple1.xml"
+
Main> runX (readDocument [ withValidate no] "simple1.xml"
 
>>> deep (isElem >>> hasName "guest"))
 
>>> deep (isElem >>> hasName "guest"))
 
</hask>
 
</hask>
Line 72: Line 72:
 
to see the XML structures inside the guest tags. The <hask>deep</hask>
 
to see the XML structures inside the guest tags. The <hask>deep</hask>
 
filter traverses the XML structures recursively and applies the filter
 
filter traverses the XML structures recursively and applies the filter
in its parameter to all the underlying structures. <hask>(a_validate,v_0)</hask>
+
in its parameter to all the underlying structures. <hask>withValidate no</hask>
 
turns off validation -- since the XML technically isn't well formed.
 
turns off validation -- since the XML technically isn't well formed.
   
Line 107: Line 107:
   
 
<hask>
 
<hask>
Main> runX (readDocument [(a_validate,v_0)] "simple1.xml" >>> getGuest)
+
Main> runX (readDocument [withValidate no] "simple1.xml" >>> getGuest)
 
</hask>
 
</hask>
   
Line 137: Line 137:
   
 
> main = do
 
> main = do
> guests <- runX (readDocument [(a_validate,v_0)] "simple1.xml"
+
> guests <- runX (readDocument [withValidate no] "simple1.xml"
 
> >>> getGuest2)
 
> >>> getGuest2)
 
> print guests
 
> print guests

Revision as of 17:43, 24 January 2011

The Data

Save this data to "simple1.xml"

<guestbook>
  <guest>
    <fname>John</fname>
    <lname>Steinbeck</lname>
  </guest>
  <guest>
    <fname>Henry</fname>
    <lname>Ford</lname>
  </guest>
  <guest>
    <fname>Andrew</fname>
    <lname>Carnegie</lname>
  </guest>
  <guest>
    <fname>Anton</fname>
    <lname>Chekhov</lname>
  </guest>
  <guest>
    <fname>George</fname>
    <lname>Washington</lname>
  </guest>
  <guest>
    <fname>William</fname>
    <lname>Shakespeare</lname>
  </guest>
  <guest>
    <fname>Nathaniel</fname>
    <lname>Hawthorne</lname>
  </guest>
</guestbook>

An unlikely list, but it will suffice for our purposes.

The Code

First, a quick overview of the necessary imports and compiler declarations:

The LANGUAGE pragma allows us to specifically enable certain language extensions. The Arrows option provides the special Arrow syntax, and the NoMonomorphismRestriction option eliminates the need for explicit type signatures on our filters.

> {-# LANGUAGE Arrows, NoMonomorphismRestriction #-} > import Text.XML.HXT.Core

The XML will be parsed directly into this data-structure:

> data Guest = Guest { firstName, lastName :: String } > deriving (Show, Eq)

I find it helpful to get a feel for the combinators at the GHCi prompt. At this point, you may want to start GHCi and load the code so far. Then you can use commands like:

Main> runX (readDocument [ withValidate no] "simple1.xml" >>> deep (isElem >>> hasName "guest"))

to see the XML structures inside the guest tags. The deep filter traverses the XML structures recursively and applies the filter in its parameter to all the underlying structures. withValidate no turns off validation -- since the XML technically isn't well formed.

With the "guest" structures in hand, the filter can be refined to pick out the first and last name text and construct a Guest value. The proc syntax is introduced here to show how it can be done conveniently.

> getGuest = deep (isElem >>> hasName "guest") >>> > proc x -> do > fname <- getText <<< getChildren <<< deep (hasName "fname") -< x > lname <- getText <<< getChildren <<< deep (hasName "lname") -< x > returnA -< Guest { firstName = fname, lastName = lname }

If you are familiar with monadic do-syntax, you've probably noticed some similarities already. There is a do, and there is a <-. But there are some new operators too: -<, returnA, and this introduced variable x.

If you squint a bit you'll notice that there seems to be a bit of an "arrow" feel to the syntax:

... <- ... <<< ... <<< ... -< ...

That's done purposefully, you can think of the XML structures flowing through the combinators in the direction the arrow is pointing. Binding is still done with <-. returnA returns the value to the Arrow much like return does to a monad.

Test it out in GHCi:

Main> runX (readDocument [withValidate no] "simple1.xml" >>> getGuest)

There is some repetition in the above code. Let's factor it out into useful combinators.

> atTag tag = deep (isElem >>> hasName tag) > text = getChildren >>> getText

And rewrite the example, much cleaner.

> getGuest2 = atTag "guest" >>> > proc x -> do > fname <- text <<< atTag "fname" -< x > lname <- text <<< atTag "lname" -< x > returnA -< Guest { firstName = fname, lastName = lname }

Hopefully, at this point it should be easy to follow the code, with the more appropriately named functions.

> main = do > guests <- runX (readDocument [withValidate no] "simple1.xml" > >>> getGuest2) > print guests