仅限于xpath或甚至Select-Xml,否则如何打印书名?
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> 我记得,示例xml来自微软的一个示例和帮助文件中的foreach习惯用法。
另请参阅:
How to load or read an XML file using ConvertTo-Xml and Select-Xml?
发布于 2020-12-13 11:38:47
Xml/Json是PowerShell的一等公民。
至于这一点:
‘限制为xpath或甚至Select-Xml -否则如何打印书名?
xpath不是cmdlet/函数,它是Select-Xml的开关/参数。这是在帮助文件以及许多其他web位置中定义的。
根据我上面的评论。为什么不允许你这样做:
[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
#>或
$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
#>或
$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
#>或者仅仅是标题
$BooksXml.bookstore.Book.title
# Results
<#
Pride And Prejudice
The Handmaid's Tale
Emma
Sense and Sensibility
#>当然,您可以使用xpath/Select- XML,但如上所述,直接点引用XML源对象同样容易。
不管怎样..。
始终先默认使用内置的PowerShell帮助文件。我是说,这就是他们在那里的原因。
# 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
#>很多例子也都结束了。只需使用上面的搜索框即可找到它们。例如(你的可以被看作是这个的复制品),类似于我上面所展示的:
In Powershell how do I get Select-Xml to search multiple Nodes
https://stackoverflow.com/questions/65264156
复制相似问题