Appendix C. XPath Quick Reference

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.

No comments yet

For these examples, consider the sample XML:

No comments yet

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

No comments yet

No comments yet


No comments yet

Table C.2. Comparisons

Syntax

Meaning

[ ]

Filtering, similar to the Where-Object cmdlet.

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}

and

Logical and.

or

Logical or.

not()

Logical negation.

=

equality.

!=

inequality.


No comments yet

You must sign in or register before commenting
*
*
*
*
*

Atom Icon Comments on this page or Comments on the whole book.