If I have an XML document like this:
<root> <elem name='Greeting'> Hello </elem> <elem name='Name'> Name </elem> </root>
and some Haskell type/data definitions like this:
type Name = String type Value = String data LocalizedString = LS Name Value
and I wanted to write a Haskell function with the following signature:
getLocalizedStrings :: String -> [LocalizedString]
where the first parameter was the XML text, and the returned value was:
[LS 'Greeting' 'Hello', LS 'Name' 'Name']
how would I do this?
If HaXml is the best tool, how would I use HaXml to achieve the above goal?
Thank!
I’ve never actually bothered to figure out how to extract bits out of XML documents using HaXML; HXT has met all my needs.
You’d probably like a little more error-checking (i.e. don’t just lazily use
atTaglike me; actually verify that<root>is root,<elem>is direct descendent, etc.) but this works just fine on your example.Now, if you need an introduction to Arrows, unfortunately I don’t know of any good one. I myself learned it the ‘thrown into the ocean to learn how to swim’ way.
Something that may be helpful to keep in mind is that the
proc/-<syntax is simply sugar for the basic arrow operations (arr,>>>, etc.), just likedo/<-is simply sugar for the basic monad operations (return,>>=, etc.). The following are equivalent: