libxml and XPath love is spreading

If nothing else comes out of my work on Syncato it does seem to be raising the visibility of XPath as a general XML processing tool. First it was Sam Ruby attaining enlightenment, now Simon Willison has had his own "enlightening experience" after reading my post about libxml love.

Syncato takes XPath to the extreme and uses it for all XML manipulation, not just the queries through the URL. The next version of Syncato will also add a Python XPath document creation and modification API called PathUpdate. This allows you to create and modify documents by just providing paths to the nodes you want to create or modify. You can specify a deep path and it will create the whole hierarchy. Here's a sample from my unit tests that gives a flavor of how it works, it's pretty cool and very powerful.

# create a tree and then set a node to a new value
doc = PathUpdate()
doc.addNode("/root/child")
doc.updateNode("/root/child", "text")

self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child>text</child></root>\n')

# add another child and then update both to the same value
doc.addNode("/root/child")
doc.updateNode("/root/child", "newText")

self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child>newText</child><child>newText</child></root>\n')

# Now just update the second child.
doc.updateNode("/root/child[2]", "text")

self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child>newText</child><child>text</child></root>\n')

# Now update an attribute
doc.addNode("/root/child[1]/@attr", "text")
doc.updateNode("/root/child[1]/@attr", "newText")
self.assertEqual(doc.serialize(), '<?xml version="1.0"?>\n<root><child attr="newText">newText</child><child>text</child></root>\n')
 

Posted by Kimbro Staken

Wednesday Oct 22, 2003 at 12:07 AM
Recommended Sites