Skip to content

Instantly share code, notes, and snippets.

@rdhyee
Last active December 25, 2015 01:09
Show Gist options
  • Save rdhyee/6892975 to your computer and use it in GitHub Desktop.
Save rdhyee/6892975 to your computer and use it in GitHub Desktop.
one way to use lxml.find with namespaced elements
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"from lxml import etree\n",
"\n",
"xml_string = u\"\"\"<?xml version=\"1.0\"?>\n",
"<rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n",
" xmlns:geo=\"http://www.w3.org/2003/01/geo/wgs84_pos#\"\n",
" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n",
" <geo:Point rdf:nodeID=\"aid23375570\">\n",
" <dc:description>600 Montgomery St, San Francisco CA 94111</dc:description>\n",
" <geo:long>-122.403082</geo:long>\n",
" <geo:lat>37.794628</geo:lat>\n",
" </geo:Point>\n",
"</rdf:RDF>\"\"\"\n",
"\n",
"root = etree.fromstring(xml_string)\n",
" \n",
"long = root.find(\".//{http://www.w3.org/2003/01/geo/wgs84_pos#}long\").text\n",
"lat = root.find(\".//{http://www.w3.org/2003/01/geo/wgs84_pos#}lat\").text\n",
"print long, lat\n",
"\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"-122.403082 37.794628\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# use xpath with namespace\n",
"\n",
"print root.xpath(r\"//g:long\", namespaces={'g':'http://www.w3.org/2003/01/geo/wgs84_pos#'})[0].text\n",
"print root.xpath(r\"//g:lat\", namespaces={'g':'http://www.w3.org/2003/01/geo/wgs84_pos#'})[0].text"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"-122.403082\n",
"37.794628\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It turns out that there is a good explanation on stackoverflow for the approach shown here: http://stackoverflow.com/a/4210882/7782"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment