Thank you Richard and Antoine.<div><br></div><div>I think I see the pointlessness of my ask. </div><div><br></div><div>Regards,</div><div>Kashyap<br><br><div class="gmail_quote">On Mon, Jul 30, 2012 at 4:14 AM, Richard O'Keefe <span dir="ltr"><<a href="mailto:ok@cs.otago.ac.nz" target="_blank">ok@cs.otago.ac.nz</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im"><br>
On 29/07/2012, at 6:21 PM, C K Kashyap wrote:<br>
> I am struggling with an idea though - How can I capture the parent element of each element as I parse? Is it possible or would I have to do a second pass to do the fixup?<br>
<br>
</div>Why do you *want* the parent element of each element?<br>
One of the insanely horrible aspects of the Document Object Model is that every<br>
element is nailed in place by pointers everywhere, with the result that you<br>
cannot share elements, and even moving an element was painful.<br>
I still do a fair bit of SGML/XML process in C using a "Document Value Model"<br>
library that uses hash consing, and it's so much easier it isn't funny.<br>
<br>
While you are traversing a document tree it is useful to keep track of the<br>
path from the root. Given<br>
<br>
data XML<br>
= Element String [(String,String)] [XML]<br>
| Text String<br>
<br>
you do something like<br>
<br>
traverse :: ([XML] -> [a] -> a) -> ([XML] -> String -> a) -> XML -> a<br>
traverse f g xml = loop [] xml<br>
where loop ancs (Text s) = g ancs s<br>
loop ancs e@(Element _ _ ks) = f ancs' (map (loop ancs') ks)<br>
where ancs' = e:ancs<br>
<br>
(This is yet another area where Haskell's non-strictness pays off.)<br>
If you do that, then you have the parent information available without<br>
it being stored in the tree.<br>
<br>
<br>
<br>
<br>
</blockquote></div><br></div>