Just as regular expressions are the standard way
to interact with plain text, XPath is the standard way to interact with XML.
Because of that, XPath is something you are likely to run across in your
travels. Several cmdlets support XPath queries:
Select-Xml, Get-WinEvent, and more.
While complex in its entirety, Table C.1, “Navigation and Selection” and
Table C.2, “Comparisons” give a quick overview of the XPath
concepts.
For these examples, consider the sample XML:
<AddressBook>
<Person contactType="Personal">
<Name>Lee</Name>
<Phone type="home">555-1212</Phone>
<Phone type="work">555-1213</Phone>
</Person>
<Person contactType="Business">
<Name>Ariel</Name>
<Phone>555-1234</Phone>
</Person>
</AddressBook>Table C.2. Comparisons
Syntax | Meaning |
|---|---|
| Filtering, similar to the
For example: PS > $xml | Select-Xml "//Person[@contactType = 'Personal']" |
Select -Expand Node
contactType Name Phone
----------- ---- -----
Personal Lee {Phone, Phone}
PS > $xml | Select-Xml "//Person[Name = 'Lee']" | Select -Expand Node
contactType Name Phone
----------- ---- -----
Personal Lee {Phone, Phone} |
| Logical and. |
| Logical or. |
| Logical negation. |
| equality. |
| inequality. |
No comments yet
Add a comment