restricted to xpath
or even Select-Xml
how else are the book titles printed?
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> Select-Xml "./bookstore.xml" -XPath "/bookstore/book/title" | foreach {$_.node.InnerXML}
Pride And Prejudice
The Handmaid's Tale
Emma
Sense and Sensibility
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> Select-Xml -Path "./bookstore.xml"
cmdlet Select-Xml at command pipeline position 1
Supply values for the following parameters:
XPath: /bookstore/book/title
Node Path Pattern
---- ---- -------
title /home/nicholas/powershell/bookstore.xml /bookstore/book/title
title /home/nicholas/powershell/bookstore.xml /bookstore/book/title
title /home/nicholas/powershell/bookstore.xml /bookstore/book/title
title /home/nicholas/powershell/bookstore.xml /bookstore/book/title
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> cat ./bookstore.xml
<?xml version="1.0"?>
<!-- A fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
<book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
<book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>29.95</price>
</book>
<book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
<title>Emma</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
<title>Sense and Sensibility</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
</bookstore>
PS /home/nicholas/powershell>
The sample xml
is from, as I recall, a Microsoft example and the foreach
idiom from the help file.
see also:
How to load or read an XML file using ConvertTo-Xml and Select-Xml?
Xml/Json is a first-class citizen in PowerShell.
As for this:
xpath is not a cmdlet/function, it's a switch/parameter of Select-Xml. This is defined in the help files as well as many other web locations.
As per my comment above. Why are you not allowed to just to this:
Or
Or
Or just titles
Sure, you can use xpath/Select-Xml, but as noted above, it's just as easy to dot reference the XML source object directly.
Anyway...
Always default to the built-in PowerShell help files first. I mean, that is why they are there.
Lot's of examples are all over SO as well. Just use the search box above to find them. For example (yours could bee seen as a duplicate of this one) which is similar to what I am showing above:
In Powershell how do I get Select-Xml to search multiple Nodes