使用 xpath、lxml、python 的父属性条件的元素路径
我正在使用 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>
假设我想使用条件
- PatientID="SKU55527"
- 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
- PatientID="SKU55527"
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此 XPath 表达式:
选择此节点的结果:
This XPath expression:
Results in this node selected:
如果你想原生使用 lxml 而不是 xpath:(否则,unutbu 的解决方案是完美的)
If you want to use lxml natively instead of xpath: (otherwise, unutbu's solution is perfect)