Tuesday, February 23, 2010

Using s-expressions instead of XML

Last time I needed to manipulate a large XML document I remembered Paul Graham's comment in What Made Lisp Different that programs communicating with s-expressions is an idea recently reinvented as XML. I began to wonder if I could just use s-expressions instead of having to deal with XML.

Step 0: Define an s-expression representation for XML.

 (tagname (@ attr "value" attr2 "value2")
   (tagname2)
   (tagname3 "data"))
If the attributes are optional, then that requires an extra token (@) to distinguish between attributes and the first nested tag.

If the attributes are not optional, then that requires an extra token (nil) when there are no attributes specified.

Most XML documents I've used have more tags without attributes, so I opted for using @.

Since @ can't be a tag name, if the first thing in the list (after the tag name) is a list whose car is @ then it is the XML attributes for that tag. I dubbed this representation SML (S-expression Meta Language).

UPDATE: I came up with a simpler representation.

Step 1: convert XML to s-expressions.
This seems like a job for Perl. It's great at manipulating data formats. So I wrote xml2sexp.pl which works great.

But it seems like a hack because there might be some XML syntax that it doesn't handle. XSLT was designed for transforming XML so it's a good choice for this also. So of course, I did some Googling and found this xml2sexp.xsl, but it's not complete. It can't even convert itself. So I decided to write my own. Yikes! Now I'm back to writing XML, which I was trying to avoid! I can't think of a programming language that is more unpleasant than XML. But it was a chance to learn XSLT, so I wrote xml2sexp.xsl too.

Step 2: Convert SML back to XML.
Now I'm in the Lisp world, so I can use my Lisp of choice, which happens to be Arc at the moment. So I wrote an Arc library, sml.arc, to convert SML back to XML. There's also a function to pretty-print the SML, since the SML created by the conversion from XML is pretty ugly SML.

Adios, XML! I'll never need to deal with you again. I can just use SML whenever I need to work with XML files.

16 comments:

  1. You may want to look at http://en.wikipedia.org/wiki/SXML

    ReplyDelete
  2. http://docs.plt-scheme.org/xml/index.html

    ReplyDelete
  3. Thank you.
    Seriously,
    thank you.

    This is just what I needed to see.

    ReplyDelete
  4. I prefer XML over s-expression tbh, although great info.
    I did find a good book/site for brushing up on your xml for those that don't like working with it too well!
    XMl For Dummies Homepage! and
    XML Dummies

    not saying that only dummies should use it, lol!

    ReplyDelete
  5. Thanks for a sweet article,very informative.. we will sure be back for more.It is wonderful right here. good research. I've been searched this kind of information for quite a while. thanks
    windows 7 key

    ReplyDelete
  6. Brilliant posting you have done great job, it’s so great helpful for me. And I am looking forward of this post thank you.
    search engine marketing melbourne

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. I Read it's blog comment ablut for xml.I began to wonder if I could just use s-expressions instead of having to deal with XML it's so great helpful for me. Best Regards.....Seo Company

    ReplyDelete
  9. Wow, theres some nice links there - where do you find them all? I’m going to read all those through in detail and apply a few.. Thanks for sharing!
    German silver

    ReplyDelete
  10. XML has a very important function and that is your site map construction. It`s all very well having readable and relevant content on your site, but your site map is like having the contents of your treasure chest stuck on the lid.


    how to increase website traffic

    ReplyDelete
  11. Its really Very useful post.. I hope you will continue your same best work and we will get more informative post which can helpful to us. Thanks for this!!!
    joomla extensions

    ReplyDelete
  12. I'm happy when reading through your site with up-to-date information! thanks alot and hope that you'll publish more site that are based on this website. joomla developers

    ReplyDelete
  13. Glad to visit this professional blog... I would love to learn more and more about technical and programming related things.. Great work!
    Affordable web hosting service

    ReplyDelete
  14. Yeah it was very wonderful. I actually implement this myself. Thanks a lot for enjoying this beauty article with me. It is very interesting for me to read this blog.
    marketing solutions

    ReplyDelete
  15. Also, having a high FICO rating and a perfect record as a consumer can regularly slice back various focuses off your home loan credit's advantage. In this way, it is exhorted that you tidy up your acknowledge report however much as could be expected and get your most astounding conceivable score before you apply for a home loan credit. www.usacheckcashingstore.com/san-diego

    ReplyDelete
  16. Eventually, this permits you access to cash all the more rapidly. Since these organizations understand that you can basically apply somewhere else on the off chance that they don't answer to you, they will typically endeavor to make the procedure speedy, simple and get assets to you with little bother.
    check cashing near me san diego

    ReplyDelete