如何在 Powershell 中使用 Select-Xml 打印 xml 节点元素的内容? [英] How to use Select-Xml in Powershell to print the content of an xml node element?

查看:34
本文介绍了如何在 Powershell 中使用 Select-Xml 打印 xml 节点元素的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅限于 xpath 甚至 Select-Xml 书名如何打印?

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> 

我记得,示例 xml 来自 Microsoft 示例和帮助文件中的 foreach 习惯用法.

The sample xml is from, as I recall, a Microsoft example and the foreach idiom from the help file.

另见:

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

推荐答案

Xml/Json 是 PowerShell 中的一等公民.

Xml/Json is a first-class citizen in PowerShell.

至于这个:

'仅限于 xpath 甚至 Select-Xml 书名如何打印?'

'restricted to xpath or even Select-Xml how else are the book titles printed?'


xpath 不是 cmdlet/函数,而是 Select-Xml 的开关/参数.这在帮助文件以及许多其他网站位置中都有定义.


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

$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 源对象同样容易.

Sure, you can use xpath/Select-Xml, but as noted above, it's just as easy to dot reference the XML source object directly.

无论如何...

始终默认首先使用内置的 PowerShell 帮助文件.我的意思是,这就是他们在那里的原因.

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

很多例子也都在 SO 上.只需使用上面的搜索框即可找到它们.例如(你的可能被视为这个副本的副本)这类似于我上面显示的内容:

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:

在 Powershell 中我该怎么做获取 Select-Xml 以搜索多个节点

这篇关于如何在 Powershell 中使用 Select-Xml 打印 xml 节点元素的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆