| |||||
Baking XML in the languageThis 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.
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.
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 JumpBox Virtual Appliances Virtualization Daily Grid7 Venturecast Inspirational Technology Scrollin on Dubs MC Ping - Microcontent Notfications
Archives
XML --
subscribe
Music -- subscribe Programming -- subscribe Python -- subscribe Syncato -- subscribe Photography -- subscribe Mac OS X -- subscribe General -- subscribe Canon EOS 1D -- subscribe Canon EOS 10D -- subscribe Canon EOS Digital Rebel -- subscribe Samsung Digimax V50 -- subscribe | ||||
Copyright 2002, 2003 Kimbro Staken
Powered By: Syncato 0.8 | |||||