powershell xml select attribute value where clause

折月煮酒 提交于 2021-02-05 07:57:06

问题


I have a xml file with the following elements:

<telecom use="HP" value="tel:+1-512-555-1212" />
<telecom use="WP" value="tel:+1-512-123-4567" />

this returns the value of the first node:

$qrda.ClinicalDocument.recordTarget.patientRole.telecom[0].value

However I need to be able to return the node where use="HP" and I'm not certain they'll always be in the correct order.

Any help is much appreciated.


回答1:


The non-XPATH method:

($qrda.ClinicalDocument.recordTarget.patientRole.telecom | Where {$_.use -eq "HP"}).value

Or (thanks to Tomalak's helpful comment), using the comparison statement format:

($qrda.ClinicalDocument.recordTarget.patientRole.telecom | Where use -eq "HP").value



回答2:


Use XPath. Assuming that $qrda is your XML document:

$path = "/ClinicalDocument/recordTarget/patientRole/telecom[@use='HP']"
$telecom = $qrda.SelectSingleNode($path)

since that path is pretty long and overly specific we can trim it down:

$telecom = $qrda.SelectSingleNode("//telecom[@use='HP']")


来源:https://stackoverflow.com/questions/45238987/powershell-xml-select-attribute-value-where-clause

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!