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. Tables C-1 and C-2 give a quick overview of XPath concepts.

For these examples, consider this 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-1. Navigation and selection

Syntax

Meaning

/

Represents the root of the XML tree.

For example:

PS > $xml | Select-Xml "/" | Select -Expand Node

AddressBook
-----------
AddressBook

/Node

Navigates to the node named Node from the root of the XML tree.

For example:

PS > $xml | Select-Xml "/AddressBook" | Select -Expand Node

Person
------
{Lee, Ariel}

/Node/*/Node2

Navigates to the noded named Node2 via Node, allowing any single node in between.

For example:

PS > $xml | Select-Xml "/AddressBook/*/Name" | Select -Expand Node

#text
-----
Lee
Ariel

//Node

Finds all nodes named Node, anywhere in the XML tree.

For example:

PS > $xml | Select-Xml "//Phone" | Select -Expand Node

type                                   #text
----                                   -----
home                                   555-1212
work                                   555-1213
                                       555-1234

..

Retrieves the parent node of the given node.

For example:

PS>$xml | Select-Xml "//Phone" | Select -Expand Node

type                                  #text
----                                  -----
home                                  555-1212
work                                  555-1213
                                      555-1234


PS>$xml | Select-Xml "//Phone/.." | Select -Expand Node

contactType              Name                     Phone
-----------              ----                     -----
Personal                 Lee                      {Phone, Phone}
Business                 Ariel                    555-1234

@Attribute

Accesses the value of the attribute named Attribute.

For example:

PS > $xml | Select-Xml "//Phone/@type" | Select -Expand Node

#text
-----
home
work

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.107.229