1

My XML begins like this:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="define2-0-0.xsl"?>
<ODM
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns="http://www.cdisc.org/ns/odm/v1.3"
  xmlns:def="http://www.cdisc.org/ns/def/v2.0"
  xmlns:nciodm="http://ncicb.nci.nih.gov/xml/odm/EVS/CDISC"
  ODMVersion="1.3.2"
  FileType="Snapshot"
  FileOID="SDTM-Terminology-2015-06-26.odm.xml.."
  CreationDateTime="2016-08-30T16:37:21">
    <Study OID="SDTM-Terminology-2015-06-26.odm.xml..">
    ...it goes on from there

I can parse it into am ELementTree object:

import xml.etree.ElementTree as ET
root = tree.getroot()

This code:

for child in root:
    print "Child of root:  " + child.tag

Produces:

Child of root:  {<some url>}Study

I need to traverse several levels down the tree, and I don't want to have to use 7-8 nested "for child in..." loops. I want to use XMLPath but I can't get anything to work. This code:

for node in tree.findall('./Study')
    print(node.tag)

returns nothing. I can't get it to return anything. I've tried

for node in root.findall('./Study')
    print (node.tag)

and it also produces nothing. I've tried all kinds of combinations of root & tree, with and without the leading "./". I can't get anything to work. This is my first time working with ElementTree; can someone help me?

boing
  • 499
  • 2
  • 6
  • 22
  • `namespaces = {'odm': 'http://www.cdisc.org/ns/odm/v1.3'}` then `tree.findall('.//odm:Study', namespaces)`. See duplicate link for further info. – kjhughes Mar 15 '19 at 03:18
  • ...and, please, accept some of helpful answers to the questions you've asked. Thanks. – kjhughes Mar 15 '19 at 03:20

0 Answers0