2

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 python and lxml and xpath

3 Answers 3

3
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'}
5
  • 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. Oct 19, 2010 at 14:42
  • Please, when the schema is well known, don't start XPath expressions with // operator because this trasverse all the tree.
    – user357812
    Oct 19, 2010 at 15:13
  • @Alejandro: Thanks; that's a good point. I hope you don't mind my taking your nicely formatted XPath, too.
    – unutbu
    Oct 19, 2010 at 15:24
  • Not problem. White space handling in XPath expressions is a good feature for readability.
    – user357812
    Oct 19, 2010 at 15:29
  • I learnt some new things, hope this helped some others, thanks to you all
    – user480369
    Oct 19, 2010 at 17:15
2

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"/>  
2
  • @Alejandro: +1 for a pure XPath solution. Oct 19, 2010 at 16:23
  • 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 msdn.microsoft.com/en-us/library/ms256086.aspx with some great xpath exaples
    – user480369
    Oct 19, 2010 at 17:13
1

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.