开发者

path to element with conditions on parent(s) attributes using xpath,lxml,python

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


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'}


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"/>  


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜