1

If I have the following xml:

<uima.cas.FSArray _id="7429" size="2">
<i>7409</i>
<i>7419</i>
</uima.cas.FSArray>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7342" codingScheme="SNOMEDCT" code="269435009" oid="269435009#SNOMEDCT" score="0.0" disambiguated="false" cui="C0879626" tui="T046" preferredText="Adverse effects"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7322" codingScheme="SNOMEDCT" code="157754004" oid="157754004#SNOMEDCT" score="0.0" disambiguated="false" cui="C0879626" tui="T046" preferredText="Adverse effects"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7352" codingScheme="SNOMEDCT" code="269432007" oid="269432007#SNOMEDCT" score="0.0" disambiguated="false" cui="C0879626" tui="T046" preferredText="Adverse effects"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7332" codingScheme="SNOMEDCT" code="213029005" oid="213029005#SNOMEDCT" score="0.0" disambiguated="false" cui="C0879626" tui="T046" preferredText="Adverse effects"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7362" codingScheme="SNOMEDCT" code="157762007" oid="157762007#SNOMEDCT" score="0.0" disambiguated="false" cui="C0879626" tui="T046" preferredText="Adverse effects"/>
<uima.cas.FSArray _id="7372" size="5">
<i>7362</i>
<i>7332</i>
<i>7352</i>
<i>7322</i>
<i>7342</i>
</uima.cas.FSArray>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7235" codingScheme="SNOMEDCT" code="274241003" oid="274241003#SNOMEDCT" score="0.0" disambiguated="false" cui="C0004134" tui="T184" preferredText="Ataxia"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7265" codingScheme="SNOMEDCT" code="39384006" oid="39384006#SNOMEDCT" score="0.0" disambiguated="false" cui="C0004134" tui="T184" preferredText="Ataxia"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7255" codingScheme="SNOMEDCT" code="206825002" oid="206825002#SNOMEDCT" score="0.0" disambiguated="false" cui="C0004134" tui="T184" preferredText="Ataxia"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7275" codingScheme="SNOMEDCT" code="20262006" oid="20262006#SNOMEDCT" score="0.0" disambiguated="false" cui="C0004134" tui="T184" preferredText="Ataxia"/>
<org.apache.ctakes.typesystem.type.refsem.UmlsConcept _id="7245" codingScheme="SNOMEDCT" code="158202006" oid="158202006#SNOMEDCT" score="0.0" disambiguated="false" cui="C0004134" tui="T184" preferredText="Ataxia"/>
<uima.cas.FSArray _id="7285" size="5">
<i>7245</i>
<i>7275</i>
<i>7255</i>
<i>7265</i>
<i>7235</i>

I want to get the _id and the <i> subelements from nodes uima.cas.FSArray

i.e. looking at the first node (first three lines) I would like to retrieve something like

_id    i
7429   7409
7429   7419

and similar for following uima.cas.FSArray nodes.

I realize that the same node (without attributes appears) so I am only interested in nodes with the _id element.

Here is my attempt:

#!/usr/bin/env python

import sys
import os
import xml.etree.cElementTree as ET

tree = ET.ElementTree(file=sys.argv[-1])

UMLSarr = {}
for x in tree.iterfind('uima.cas.FSArray'):
    UMLSarr[x] = x.attrib
    subArr[x] = SubElement(UMLSarr[x],"subArr",attrib='i')

but I get:

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
NameError: name 'SubElement' is not defined

I've tried various other iterations of this code but I'm running into more and more errors and hoped someone could give me a hand.

Thanks.

1 Answer 1

1
from lxml import etree

et = etree.fromstring(xml)
for array in et.xpath('//uima.cas.FSArray[@_id]'):
    print(array.xpath('@_id'), array.xpath('./i/text()'))

out:

['7429'] ['7409', '7419']
['7372'] ['7362', '7332', '7352', '7322', '7342']
['7285'] ['7245', '7275', '7255', '7265', '7235']
Sign up to request clarification or add additional context in comments.

1 Comment

@(sorry I can't spell your username) - this worked for me. thanks very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.