XML with Xen and with Python

Posted 3 Dec 2004 at 21:39 UTC by wspace Share This

Here is a comment on the paper "Programming with Circles, Triangles and Rectangles" by Erik Meijer et al. Perhaps interesting for XML programmers.


Here is a comment on the paper "Programming with Circles, Triangles and Rectangles" by Erik Meijer, Wolfram Schulte and Gavin Bierman. Google will find it quickly if you try.

In the paper they introduce Xen, an extension to C# for better XML support. They show how Lifting, Filtering and Apply-to-all can be done so much better in C# with Xen.

How is Python, my favorite programming language, doing in this respect? Let's see:

Python for XML ==============

Lifting -------

The example: "bib.books.title" [[an expression for all titles of books in bib]]

(stuff in double quotes "..." is suggested Xen code from the article by E.Meijer et al., stuff in double square brackets [[...]] is the intended meaning. Python code is shown after >>> , the prompt from the Python interactive interpreter.)

We can do this "Lifting" in Python with list comprehension: >>> [b.title for b in bib.books]

Filtering ---------

The example: "bib.books[it.year >= 1998]" [[An expression for the books from 1998 or later]]

This can also be done with Python list comprehensions. >>> [b for b in bib.books if b.year >= 1998]

Apply-to-all ------------

The example: "bib.books.{print it}" [[print all books: execute a code block for every book in .books.]]

I would do it like this in Python:

for b in bib.books: print b

looks much clearer to me than the .{...} stuff. Oh and maybe the new generator expressions can be used for this, but they are new to me and I can hardly imagine clearer syntax. Anyone?

All that is needed for this to work in Python is that you collect XML attributes in object attributes; and that you give a name to the XML children of an XML element and put them as a list into an object attribute with that name. Go wild and even define new classes for it dynamically, or beforehand if you have the XML DTD or Schema. Not too hard a programming exercise; who is first?

As for the other content of the article it is my opinion that they pay way too much attention to data-models and types, and then strangle themselves trying to fit one data-model onto another one, and a statically typed one too for that! (C#, Java, even Visual Basic is mentioned for laughing out loud). Better try to get the job done first.

They did this work paid by microsoft.research. I think microsoft.research better pay some more attention to Python. Or are they? Are the "competitive edge" rumors true?

And there are possibly other elegant solutions in languages like Ruby or even Perl. I am interested in examples from those. Better than trying to cram it all into a rigid statically typed language.


Xen not to be confused with xen?, posted 4 Dec 2004 at 18:02 UTC by lkcl » (Master)

Xen, not to be confused with Xen.

non-confusing link to Xen not Xen greatly appreciated!

It is called Cw now, posted 4 Dec 2004 at 20:52 UTC by wspace » (Journeyer)

First it was X#, then Xen, and I just learned that now it is part of Comega. See http://research.microsoft.com/comega/
My sympathy goes to your Xen!

ah goood., posted 5 Dec 2004 at 10:25 UTC by lkcl » (Master)

because research.microsoft.com sponsored xen, the other-xen, bizarrely enough.

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!

X
Share this page