HaXml 1.13 -> 1.22 upgrade

I'm trying to migrate one my programs from the old HaXml API to the new. Please, someone save me. I'm currently stuck with this, which works in 1.13. All of the filters work on Content, so I make some from the root element with the "root_elem = CElem root" line. -- |Takes an XML String as an argument, and returns the -- status that was parsed from it. Should only be used -- on XML string where a <status> is a top-level element. parse_status :: String -> [Status] parse_status xml_data = catMaybes maybe_status where (Document _ _ root _) = xmlParse xml_file_name xml_data root_elem = CElem root status_element = (single_status root_elem) maybe_status = map status_from_content status_element In the new API, xmlParse returns the root element with type (Element i) rather than just Element. And the Content constructor I have to use is (CElem (Element i) i), but I have no way to pass the correct 'i' to it. I just want to parse a few elements from an XML file.

On Sun, Dec 11, 2011 at 12:08 AM, Michael Orlitzky
I'm trying to migrate one my programs from the old HaXml API to the new. Please, someone save me.
I'm currently stuck with this, which works in 1.13. All of the filters work on Content, so I make some from the root element with the "root_elem = CElem root" line.
-- |Takes an XML String as an argument, and returns the -- status that was parsed from it. Should only be used -- on XML string where a <status> is a top-level element. parse_status :: String -> [Status] parse_status xml_data = catMaybes maybe_status where (Document _ _ root _) = xmlParse xml_file_name xml_data root_elem = CElem root status_element = (single_status root_elem) maybe_status = map status_from_content status_element
In the new API, xmlParse returns the root element with type (Element i) rather than just Element. And the Content constructor I have to use is (CElem (Element i) i), but I have no way to pass the correct 'i' to it.
It looks like the function 'xmlParse' returns a value of type 'Document Posn', according to the API docs. I'm guessing the 'Posn' value is used to annotate the position in the source document a particular piece of XML came from, so you can report errors better. Since the pretty-printing functions ignore it, you can replace it with whatever you want, even with a value of a different type if you have a need to annotate the tree.
I just want to parse a few elements from an XML file.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 12/11/2011 01:36 AM, Antoine Latter wrote:
It looks like the function 'xmlParse' returns a value of type 'Document Posn', according to the API docs. I'm guessing the 'Posn' value is used to annotate the position in the source document a particular piece of XML came from, so you can report errors better.
Since the pretty-printing functions ignore it, you can replace it with whatever you want, even with a value of a different type if you have a need to annotate the tree.
Thanks, I was able to get it working after a little sleep/coffee. The migration guide says to replace all of the 'i' with () if you don't care about them, so I tried that, but it doesn't work in this case: the two 'i' in (CElem (Element i) i) have to match. The only way I see to construct a Posn is with noPos, so I stuck that in there. It's probably not correct, but it compiles and runs, so it's correct =)

The extra parameter "i" is for "information" attached to each node of the tree. As you have correctly guessed, the parser fills in this field with positional information relating to the original source document, which is useful for instance if you are validating or checking the original document. When building new parts of a document, it is perfectly fine to attach "noPos".
You can alternatively replace all of the informational items in the tree, with for instance "fmap (const ())" if you don't care about them.
The information fields are useful for other purposes though, e.g. to hold the relevant xmlns namespace for subtrees; or to distinguish added/removed/modified subtrees in a diff-like viewer.
Regards,
Malcolm
On 11/12/2011, at 14:56, Michael Orlitzky
On 12/11/2011 01:36 AM, Antoine Latter wrote:
It looks like the function 'xmlParse' returns a value of type 'Document Posn', according to the API docs. I'm guessing the 'Posn' value is used to annotate the position in the source document a particular piece of XML came from, so you can report errors better.
Since the pretty-printing functions ignore it, you can replace it with whatever you want, even with a value of a different type if you have a need to annotate the tree.
Thanks, I was able to get it working after a little sleep/coffee.
The migration guide says to replace all of the 'i' with () if you don't care about them, so I tried that, but it doesn't work in this case: the two 'i' in (CElem (Element i) i) have to match.
The only way I see to construct a Posn is with noPos, so I stuck that in there. It's probably not correct, but it compiles and runs, so it's correct =)
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 12/12/11 02:42, Malcolm Wallace wrote:
The extra parameter "i" is for "information" attached to each node of the tree. As you have correctly guessed, the parser fills in this field with positional information relating to the original source document, which is useful for instance if you are validating or checking the original document. When building new parts of a document, it is perfectly fine to attach "noPos".
You can alternatively replace all of the informational items in the tree, with for instance "fmap (const ())" if you don't care about them.
The information fields are useful for other purposes though, e.g. to hold the relevant xmlns namespace for subtrees; or to distinguish added/removed/modified subtrees in a diff-like viewer.
Regards, Malcolm
Thanks, I understand what's going on now. I like the noPos solution because the other ones make the reader think there's something important going on; with noPos, I can just comment "gotta give it a Posn, and this is the only one I know how to make."
participants (3)
-
Antoine Latter
-
Malcolm Wallace
-
Michael Orlitzky