XML modification

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

XML modification

Andrew Coppin
Hi guys.

I've got a folder with about 80 XML files in it. I want to take each
file and make specific modifications to it. (Mostly just finding
specific attributes and changing their values to make then all consistent.)

Now I guess it wouldn't take me /that/ long to code something from
scratch. But does anybody have a better suggestion for tools or
libraries that might be useful?

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Fwd: XML modification

Dmitry Olshansky
Personally I prefer xml-enumerator. You can work with xml-stream or with DOM through cursor (like XPath).

I think that in your case working with stream is preferrable.

You can just make an Enumeratee like

>import qualified Data.Enumerator.List as EL

> myEnum = EL.map f
>    where
>       f (EventBeginElement n ats) = ... -- parse attributes
>       f e = e



2011/11/23 Andrew Coppin <[hidden email]>
Hi guys.

I've got a folder with about 80 XML files in it. I want to take each file and make specific modifications to it. (Mostly just finding specific attributes and changing their values to make then all consistent.)

Now I guess it wouldn't take me /that/ long to code something from scratch. But does anybody have a better suggestion for tools or libraries that might be useful?

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: XML modification

Jon Fairbairn
In reply to this post by Andrew Coppin
Andrew Coppin <[hidden email]> writes:

> I've got a folder with about 80 XML files in it. I want to
> take each file and make specific modifications to it.
> (Mostly just finding specific attributes and changing their
> values to make then all consistent.)
>
> Now I guess it wouldn't take me /that/ long to code
> something from scratch. But does anybody have a better
> suggestion for tools or libraries that might be useful?

HaXml

Before google earth exposed a facility for computing path
lengths, I wrote my own using HaXml. The guts of it look like
this:

main …
         xml <- fmap (xmlParse filename) (readFile filename)
         print_path_length xml

print_path_length (Document _ _ content _)
    = print $ compute_length $ coordinate_string_to_list coordinate_string
      where coordinate_string
                = singleCString
                  . (txt `o` children `o` deep (tag "coordinates"))
                  $ (CElem content)

which gives some idea of the flavour.

--
Jón Fairbairn                                 [hidden email]
http://www.chaos.org.uk/~jf/Stuff-I-dont-want.html  (updated 2010-09-14)


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: XML modification

Andrew Coppin
On 23/11/2011 10:14 AM, Jon Fairbairn wrote:
> Andrew Coppin<[hidden email]>  writes:
>
>> I've got a folder with about 80 XML files in it. I want to
>> take each file and make specific modifications to it.

> HaXml

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"?

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: XML modification

Andrew Coppin
On 23/11/2011 12:58 PM, Andrew Coppin wrote:

> On 23/11/2011 10:14 AM, Jon Fairbairn wrote:
>> HaXml
>
> 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
omission.

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!

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: XML modification

Jon Fairbairn
Andrew Coppin <[hidden email]> writes:

> On 23/11/2011 12:58 PM, Andrew Coppin wrote:
>> On 23/11/2011 10:14 AM, Jon Fairbairn wrote:
>>> HaXml
>>
>> 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 omission.
>
> 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!

I think these observations should be addressed to Malcolm
Wallace.

--
Jón Fairbairn                                 [hidden email]



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe