how to use the cmdlet Select-Xml interactive prompt?
Asked Answered
S

1

0

How is Select-Xml used as below:

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> 

by entering the parameters and Xpath through the REPL as above. Certainly:

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.

this question was heavily edited.

I don't expect the interactive prompt is much used.

Should anyone use it, great. else, the question is answered for my purposes.

Smiga answered 12/12, 2020 at 11:18 Comment(2)
I think if you want to specify a path use e.g. Select-Xml -Path .\bookstore.xml -XPath //book/titlePsychometry
So, this is like your other post. #65264656, that I responded to. So, are we doing your homework for/with you? ;-}Wilhelmstrasse
P
1

The interactive prompt isn't the greatest. As documented, -Xml asks for an object of type [xml], while -Path just asks for the filename. There are different parameter sets. In this order, the parameter names are optional. If the xpath has square brackets you'll have to quote it.

[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

Using the pipe:

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

$xml | select-xml //title
Polyp answered 12/12, 2020 at 16:11 Comment(4)
can you demonstrate how to use the interactive prompt?Smiga
@NicholasSaunders hmm I tried $xml and it didn't seem to workPolyp
no worries and don't stress. you gave me more than enough to chew on. the interactive prompt is more a a curiosity than anything else. I'll leave it open for a bit.Smiga
@NicholasSaunders it's definitely not optimal in this casePolyp

© 2022 - 2024 — McMap. All rights reserved.