[Haskell-cafe] XML modification
andrewcoppin at btinternet.com
Wed Nov 23 18:06:33 CET 2011
On 23/11/2011 12:58 PM, Andrew Coppin wrote:
> On 23/11/2011 10:14 AM, Jon Fairbairn wrote:
> Mmm. That looks very promising...
>> which gives some idea of the flavour.
> OK. So it looks like processXmlWith is the function I want, if I'm going
> to read one file and create another from it. So now I just need to
> figure out which combinators I need. (The documentation seems a bit
> thin.) Can you show me a snippet for how I would find [one] element
> named "foo" and change its "bar" attribute to have the value "5"?
Well, from what I've been able to gather, HaXml has a really nice filter
combinator library. However...
Weird thing #1: processXmlWith handles the common case of loading a file
from disk, filtering it, and saving the result to disk again. However,
it does this based on CLI arguments. There is no function anywhere that
I can find which allows the host program to specify what files to
process. If you want to do that, you have to reimplement most of the
body of this function all over again yourself. That seems a strange
Weird thing #2: There are absolutely no filters for dealing with
attributes. I couldn't find anything anywhere that says "apply this
function to all the attributes of this element". I can find a function
to /replace/ an element's attributes without regard to what existed
before. But even something as trivial as adding an additional attribute
while keeping the existing ones doesn't appear to be supported at all.
Fortunately it turns out to not be especially hard to read the source
for the replace-attributes function and change it to do what I want.
But, again, it seems a rather large and obvious ommission. (I'm guessing
that since attributes are key/value pairs and not "content", you would
need a seperate "attribute filter" type, which is different from the
existing content filters. Even so, it shouldn't be /that/ hard to do...)
Anyway, the important thing is, Haskell (and more specifically HaXml)
let me accomplish the task I wanted without too much fuss. It's
/certainly/ faster than editing 80 files by hand in a text editor!
More information about the Haskell-Cafe