首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用cmdlet Select-Xml交互式提示符?

如何使用cmdlet Select-Xml交互式提示符?
EN

Stack Overflow用户
提问于 2020-12-12 19:18:21
回答 1查看 118关注 0票数 0

Select-Xml的用法如下:

代码语言:javascript
复制
PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> Select-Xml

cmdlet Select-Xml at command pipeline position 1
Supply values for the following parameters:
Xml[0]: ./bookstore.xml
Xml[1]: 
XPath: /bookstore
Select-Xml: Cannot bind parameter 'Xml'. Cannot convert the "./bookstore.xml" value of type "System.String" to type "System.Xml.XmlNode".
PS /home/nicholas/powershell> 

如上所述,通过REPL输入参数和Xpath。当然:

代码语言:javascript
复制
PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> $doc = New-Object xml                                                                   
PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> $doc.Load( (Resolve-Path ./bookstore.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> 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> 

works fine

这个问题经过了大量的编辑。

我并不期望交互式提示符被广泛使用。

如果有人使用它,那太好了。否则,这个问题的答案就是为了我的目的。

EN

回答 1

Stack Overflow用户

发布于 2020-12-13 00:11:41

交互式提示符并不是最好的。正如文档所述,-Xml请求xml类型的对象,而-Path只请求文件名。有不同的参数集。按此顺序,参数名称是可选的。如果xpath包含方括号,则必须将其引起来。

代码语言:javascript
复制
[xml]$xml = get-content bookstore.xml
select-xml -XPath /bookstore/book/title -Xml $xml

Node  Path        Pattern
----  ----        -------
title InputStream /bookstore/book/title
title InputStream /bookstore/book/title
title InputStream /bookstore/book/title
title InputStream /bookstore/book/title


select-xml -XPath /bookstore/book/title -Path bookstore.xml

Node  Path                                Pattern
----  ----                                -------
title C:\Users\ccfadmin\foo\bookstore.xml /bookstore/book/title
title C:\Users\ccfadmin\foo\bookstore.xml /bookstore/book/title
title C:\Users\ccfadmin\foo\bookstore.xml /bookstore/book/title
title C:\Users\ccfadmin\foo\bookstore.xml /bookstore/book/title

使用管道:

代码语言:javascript
复制
# piping to -Content, it has to be one whole string
get-content -raw bookstore.xml | select-xml //title

$xml | select-xml //title
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65264292

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档