问题
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