0

仅限于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我记得该示例来自 Microsoft 示例和foreach帮助文件中的习语。

也可以看看:

如何使用 ConvertTo-Xml 和 Select-Xml 加载或读取 XML 文件?

4

1 回答 1

0

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

很多例子也都是如此。只需使用上面的搜索框即可找到它们。例如(你的可以看作是这个的副本),这与我上面显示的类似:

在 Powershell 中,如何让 Select-Xml 搜索多个节点

于 2020-12-13T03:38:47.467 回答