使用 xpath、lxml、python 的父属性条件的元素路径

发布于 2024-09-28 03:03:24 字数 1287 浏览 3 评论 0原文

我正在使用 lxml 进行项目。这是一个示例 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>

假设我想使用条件

  1. PatientID="SKU55527"
  2. StudyInstanceUID="25.2.9.2.1107.5.1.4.49339.30000007010207164403100000013"; 访问系列元素;

我的结果将是:

<Series SeriesInstanceUID="2.16.840.1.113669.1919.1198835358"/>  

如果我能理解这个解决方案,那么我将在学习 xml 方面更近一步。 PS 我正在使用 python、lxml 和 xpath

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ら栖息 2024-10-05 03:03:24
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'}
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'}
九命猫 2024-10-05 03:03:24

此 XPath 表达式:

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

选择此节点的结果:

<Series 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"/>  
野侃 2024-10-05 03:03:24

如果你想原生使用 lxml 而不是 xpath:(否则,unutbu 的解决方案是完美的)

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文