【问题标题】:how to use the cmdlet Select-Xml interactive prompt?如何使用 cmdlet Select-Xml 交互提示?
【发布时间】:2021-03-23 15:11:37
【问题描述】:

Select-Xml 的使用方法如下:

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。当然:

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> 

工作fine

这个问题被大量编辑。

我不希望交互式提示被广泛使用。

如果有人使用它,太好了。否则,为了我的目的,这个问题得到了回答。

【问题讨论】:

  • 我想如果你想指定一个路径使用例如Select-Xml -Path .\bookstore.xml -XPath //book/title
  • 所以,这就像你的其他帖子一样。 stackoverflow.com/questions/65264156/…,我回复了。那么,我们是否为您/与您一起做作业? ;-}

标签: xml powershell xpath xquery select-xml


【解决方案1】:

交互式提示不是最好的。如文档所述,-Xml 要求提供 [xml] 类型的对象,而 -Path 只要求提供文件名。有不同的参数集。按此顺序,参数名称是可选的。如果 xpath 有方括号,则必须引用它。

[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

使用管道:

# piping to -Content, it has to be one whole string
get-content -raw bookstore.xml | select-xml //title

$xml | select-xml //title

【讨论】:

  • 你能演示一下如何使用交互式提示吗?
  • @NicholasSaunders 嗯,我尝试了 $xml,但似乎没有用
  • 不用担心,不用紧张。你给了我足够多的咀嚼。交互式提示比其他任何东西都更像是一种好奇心。我会暂时打开它。
  • @NicholasSaunders 在这种情况下绝对不是最佳选择
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-28
  • 1970-01-01
  • 2020-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多