问题
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?
回答1:
Xml/Json is a first-class citizen in PowerShell.
As for this:
'restricted to xpath or even Select-Xml how else are the book titles printed?'
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:
[XML]$BooksXml = @"
<?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>
"@
$BooksXml.bookstore.Book |
Format-Table -AutoSize
# Results
<#
$BooksXml.bookstore.Book |
Format-Table -AutoSize
genre publicationdate ISBN title author price
----- --------------- ---- ----- ------ -----
novel 1997 1-861001-57-8 Pride And Prejudice author 24.95
novel 1992 1-861002-30-1 The Handmaid's Tale author 29.95
novel 1991 1-861001-57-6 Emma author 19.95
novel 1982 1-861001-45-3 Sense and Sensibility author 19.95
#>
Or
$BooksXml.bookstore.Book |
Select-Object -Property title, ISBN, author, Price
# Results
<#
title ISBN author price
----- ---- ------ -----
Pride And Prejudice 1-861001-57-8 author 24.95
The Handmaid's Tale 1-861002-30-1 author 29.95
Emma 1-861001-57-6 author 19.95
Sense and Sensibility 1-861001-45-3 author 19.95
#>
Or
$BooksXml.bookstore.Book |
Select-Object -Property title, ISBN, Price,
@{
Name = 'author'
Expression = {"$($PSItem.Author.'first-name') $($PSItem.Author.'last-name')"}
}
# Results
<#
title ISBN price author
----- ---- ----- ------
Pride And Prejudice 1-861001-57-8 24.95 Jane Austen
The Handmaid's Tale 1-861002-30-1 29.95 Margaret Atwood
Emma 1-861001-57-6 19.95 Jane Austen
Sense and Sensibility 1-861001-45-3 19.95 Jane Austen
#>
Or just titles
$BooksXml.bookstore.Book.title
# Results
<#
Pride And Prejudice
The Handmaid's Tale
Emma
Sense and Sensibility
#>
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.
# Get specifics for a module, cmdlet, or function
(Get-Command -Name-Select-Xml).Parameters
(Get-Command -Name-Select-Xml).Parameters.Keys
Get-help -Name-Select-Xml -Examples
# Results
<#
$Path = "$Pshome\Types.ps1xml"
$XPath = "/Types/Type/Members/AliasProperty"
Select-Xml -Path $Path -XPath $Xpath | Select-Object -ExpandProperty Node
[xml]$Types = Get-Content $Pshome\Types.ps1xml
Select-Xml -Xml $Types -XPath "//MethodName"
$Namespace = @{command = "http://schemas.microsoft.com/maml/dev/command/2004/10"; maml = "http://schemas.microsoft.com/maml/2004/10"; dev =
$Path = "$Pshome\en-us\*dll-Help.xml"
$Xml = Select-Xml -Path $Path -Namespace $Namespace -XPath "//command:name"
$Xml | Format-Table @{Label="Name"; Expression= {($_.node.innerxml).trim()}}, Path -AutoSize
$Xml = @"
Select-Xml -Content $Xml -XPath "//edition" | foreach {$_.node.InnerXML}
$Xml | Select-Xml -XPath "//edition" | foreach {$_.node.InnerXML}
$SnippetNamespace = @{snip = "http://schemas.microsoft.com/PowerShell/Snippets"}
Select-Xml -Path $Home\Documents\WindowsPowerShell\Snippets -Namespace $SnippetNamespace -XPath "//snip:Title" | foreach {$_.Node.Innerxml}
#>
@"
<?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>
"@ |
Select-Xml -XPath "//book" |
foreach {$PSItem.node.InnerXML}
# Results
<#
<title>Pride And Prejudice</title><author><first-name>Jane</first-name><last-name>Austen</last-name></author><price>24.95</price>
<title>The Handmaid's Tale</title><author><first-name>Margaret</first-name><last-name>Atwood</last-name></author><price>29.95</price>
<title>Emma</title><author><first-name>Jane</first-name><last-name>Austen</last-name></author><price>19.95</price>
<title>Sense and Sensibility</title><author><first-name>Jane</first-name><last-name>Austen</last-name></author><price>19.95</price>
#>
(@"
<?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>
"@ | Select-Xml -XPath '//book').Node.title
# Results
<#
Pride And Prejudice
The Handmaid's Tale
Emma
Sense and Sensibility
#>
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
来源:https://stackoverflow.com/questions/65264156/how-to-use-select-xml-in-powershell-to-print-the-content-of-an-xml-node-element