Baking XML in the language

This is a wish list item, call it a dream maybe, but I really want to see XML support baked directly into a programming language. Python in particular would be great. Here's what I want.

  • Seamless XML support. Never having to explicitly parse an XML document.
  • XPath as a native language construct.
  • Dynamic conversion between text and parsed representations of the XML.
  • XPath manipulation for XML modifications.

Here's an example of the flavor that I'm looking for. This is pseudo Python though what I'm thinking may be considered un-pythonic. That's not my concern though, it's just a concept.

doc = "<item><title>example</title><description>just an example</description></item>"
doc2 = "<item><title>example2</title><description>just another example</description></item>"

# get the title as a string
title = doc/item/title/text()
print title

# get the title as a node
title = doc/item/title
print title

# a collection of documents
docs = [doc, doc2]

# find all the titles in the collection as strings
titles = docs/item/title/text()
print titles

# find the node for the title that equals 'example'
titles = docs/item[title = 'example']/title
print titles

# find all items that contain the word another in their description
item = docs/item[contains(description, 'another')]
print item

# Use dynamic conversion to treat the XML as a string and 
# rename the description element to body
doc = re.sub("^description", "body", doc)
print doc

# append some new content to the body
doc/item/title/body += " with new content"
print doc

# Delete the title element
doc/item -= title
print doc

# append the new title
doc/item += "<title>new title</title>"
print doc

# delete the title again and then insert a new one before body
doc/item -= title
doc/item/body <= "<title>new title inserted before</title>"
print doc

# delete the title again and then insert a new one after body
doc/item -= title
doc/item/body => "<title>new title inserted after</title>"
print doc

If you could run this program here's what you would get as output.

example
<title>example</title>
["example", "example2"]
[<title>example</title>]
[<item><title>example</title><body>just an example with new content</body></item>]
<item><title>example</title><body>just an example</body></item>
<item><title>example</title><body>just an example with new content</body></item>
<item><body>just an example</body></item>
<item><body>just an example</body><title>new title</title></item>
<item><title>new title inserted before</title><body>just an example</body></item>
<item><body>just an example</body><title>new title inserted after</title></item>

The XPath modification syntax is an experimental one I came up with sometime last year but never implemented. Here's how it translates.

  • += is append
  • -= is delete
  • <= is insert before
  • >= is insert after
  • = is replace
  • ~= is rename node

BTW, I am aware of one language implementation that does some of what I want. The XQuery implementation that's part of Sherlock on Mac OS X has the dynamic conversion between text and an in memory representation of the XML. It doesn't have a modification syntax though.

Posted by Kimbro Staken

Thursday Oct 30, 2003 at 10:17 PM
Recommended Sites