I'm reading an XML with python and ElementTree and am struggling with xmlns and xsi tags.
The top of my XML looks like this.
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="website"?>
<SurveyGroup xmlns:xsi="website2" xmlns:xsd="website3" xsi:schemaLocation="website4 website5" xmlns="website6">
<Survey>
<Header>
I follow the ET process
tree = ET.parse(xmlfile)
root = tree.getroot()
The issue is the xlmns or xsi data appear to be interring with this. I'm unable to access the elements as children of this root and if I print root I get
<Element '{website}SurveyGroup' at 0x00000278FCC85120>
If I change the row to just be <SurveyGroup> i don't get this issue.
All the elements in your XML document exist in a particular namespace -- either one applied with a specific prefix (like
xsi:schemaLocation), or, for elements without a namespace prefix, the defaultwebsite6namespace (set by thexmlns=website6annotation).If you want to look up elements in that document, you need to specify the appropriate namespace. There are a couple of ways of doing this. You can include the namespace literally in curly brackets, like this:
You can also refer to namespaces via a namespace prefix:
Here, we map the prefix
footo thewebsite6namespace, so we can use thefoo:prefix on element names.You can set a default namespace in your queries by add an entry to your
namespacesdictionary with an empty key: