Re: XML Paths in Programming Languages

Regarding namespaces and Amara's XPath functionality, when you construct an object you can send a "prefixes" keyword argument that declares the namespace prefixes you want to use. For example if I had an Atom document where there were different prefixes such as "a" and "atom", I could normalize these to whatever I want. For example:


xml = """<entry xmlns="http://www.w3.org/2005/Atom"
    xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:a="http://www.w3.org/2005/Atom">
  <title>atom title</title>
  <id<urn:some-id-here</atom>
  <a:link rel="self" href="http://host.com/blog/this-entry.atom" />
  <atom:link rel="alternate" href="http://host.com/blog/this-entry" />
  <link rel="edit" href="http://host.com/blog/edit/13" />
  <content type="xhtml>
    <div xmlns="http://www.w3.org/1999/xhtml">some content</div>
  </a:content>
</entry>"""

atom_ns = u'http://www.w3.org/2005/Atom'
common_prefixes = {
    (u'atom', atom_ns),
}
doc = amara.parse(xml, prefixes=common_prefixes)
links = doc.xml_xpath(u'/atom:entry/atom:link')

This can be helpful b/c if you have some elements with prefixes, some without and some with different prefixes, it all just works. This can be helpful then cleaning up the namespaces to make them consistent.

I'm glad you looked at Amara. It truly has made working with XML a breeze.

Reply

The content of this field is kept private and will not be shown publicly.