tags:

views:

45

answers:

3

Hello

I am working on project using lxml. here is a sample xml

<PatientsTree>
  <Patient PatientID="SKU065427">    
    <Study StudyInstanceUID="25.2.9.2.1107.5.1.4.49339.30000006050107501192100000001">
      <Series SeriesInstanceUID="2.16.840.1.113669.1919.1176798690"/>
      <Series SeriesInstanceUID="2.16.840.1.113669.1919.1177084041"/>
      <Series SeriesInstanceUID="25.2.9.2.1107.5.1.4.49339.30000006050108064034300000000"/>
    </Study>    
  </Patient>
  <Patient PatientID="SKU55527">
    <Study StudyInstanceUID="25.2.9.2.1107.5.1.4.49339.30000006120407393721800000007">
      <Series SeriesInstanceUID="2.16.840.1.113669.1919.1198835144"/>
    </Study>
    <Study StudyInstanceUID="25.2.9.2.1107.5.1.4.49339.30000007010207164403100000013">
      <Series SeriesInstanceUID="2.16.840.1.113669.1919.1198835358"/>    
  </Patient>
</PatientsTree>

Suppose I want to get to the series element with conditions

  1. PatientID="SKU55527"
  2. StudyInstanceUID="25.2.9.2.1107.5.1.4.49339.30000007010207164403100000013";

My result will be :

<Series SeriesInstanceUID="2.16.840.1.113669.1919.1198835358"/>  

If I can understand this solution then I will move one step closer in learning xml. P.S I am working with pyton and lxml and xpath

+2  A: 

This XPath expression:

/PatientsTree 
  /Patient[@PatientID='SKU55527']     
    /Study[@StudyInstanceUID =
           '25.2.9.2.1107.5.1.4.49339.30000007010207164403100000013'] 
      /Series 

Results in this node selected:

<Series SeriesInstanceUID="2.16.840.1.113669.1919.1198835358"/>  
Alejandro
@Alejandro: +1 for a pure XPath solution.
Dimitre Novatchev
thanks , for some reason when I tried this syntax I was getting only empty list, I thought there was something mystic about lxml that I am still to learn. I found this page http://msdn.microsoft.com/en-us/library/ms256086.aspx with some great xpath exaples
@user480369: You are wellcome.
Alejandro
+2  A: 
import lxml.etree as le
with open('data.xml') as f:
    doc=le.parse( f )
patientID="SKU55527"
studyInstanceUID="25.2.9.2.1107.5.1.4.49339.30000007010207164403100000013"
xpath='''\
    /PatientsTree
        /Patient[@PatientID="{p}"]
            /Study[@StudyInstanceUID="{s}"]
                /Series'''.format(p=patientID,s=studyInstanceUID)
seriesInstanceUID=doc.xpath(xpath)
for node in seriesInstanceUID:
    print(node.attrib)
    # {'SeriesInstanceUID': '2.16.840.1.113669.1919.1198835358'}
unutbu
Beautifully written - I love the improved readability of the format method. I also can never help but appreciate how much lxml seems like a native library.
nearlymonolith
@unutbu: Please, when the schema is well known, don't start XPath expressions with `//` operator because this trasverse all the tree.
Alejandro
@Alejandro: Thanks; that's a good point. I hope you don't mind my taking your nicely formatted XPath, too.
unutbu
@unutbu: Not problem. White space handling in XPath expressions is a good feature for readability.
Alejandro
I learnt some new things, hope this helped some others, thanks to you all
+1  A: 

If you want to use lxml natively instead of xpath: (otherwise, unutbu's solution is perfect)

from lxml import etree as ET
tree = ET.parse('some_file.xml')
patientID="SKU55527"
studyInstanceUID="25.2.9.2.1107.5.1.4.49339.30000007010207164403100000013"
patient_node = tree.find(patientID)
if not patient_node is None:
    study_node = patient_node.find(studyInstanceUID)
    if not study_node is None:
        for child in study_node.getchildren():
            print child.attrib
            #or do whatever useful thing you want
    else:
        #didn't find the study
else:
    #didn't find the node
nearlymonolith