how to print XML documents to the REPL console when using ConvertTo-Xml in Powershell?

此生再无相见时 提交于 2021-02-11 14:25:27

问题


"almost" printing the elements to the console:

PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> $nodes = Select-Xml "/" $xml                                                                               PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> $nodes                      

Node      Path        Pattern
----      ----        -------
#document InputStream /

PS /home/nicholas/flwor> 

not quite sure how print the actual node values.

for larger context,reading in a file and converting it to xml as:

PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> $xml=./bookstore.xml   
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> $xml=ConvertTo-Xml $xml
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> $xml.ChildNodes

Version         : 1.0
Encoding        : utf-8
Standalone      : 
Value           : version="1.0" encoding="utf-8"
InnerText       : version="1.0" encoding="utf-8"
Name            : xml
LocalName       : xml
NodeType        : XmlDeclaration
PreviousSibling : 
NextSibling     : Objects
ParentNode      : #document
ChildNodes      : {}
Attributes      : 
OwnerDocument   : #document
FirstChild      : 
LastChild       : 
HasChildNodes   : False
NamespaceURI    : 
Prefix          : 
IsReadOnly      : False
OuterXml        : <?xml version="1.0" encoding="utf-8"?>
InnerXml        : 
SchemaInfo      : System.Xml.Schema.XmlSchemaInfo
BaseURI         : 
PreviousText    : 

Object : 


PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> $xml | Select-Xml –Xpath “//bookstore” 
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> $xml | Select-Xml –Xpath “/bookstore”  
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> $xml.ChildNodes | Select-Xml -XPath "/"
Select-Xml: Object reference not set to an instance of an object.
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> $xml | Get-Content                     
Get-Content: The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor>     Select-Xml -Path $Path -XPath $Xpath | Select-Object -ExpandProperty Node
Select-Xml: Cannot validate argument on parameter 'Path'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
PS /home/nicholas/flwor> Select-Xml $foo -XPath $Xpath | Select-Object -ExpandProperty Node           
Select-Xml: Cannot validate argument on parameter 'XPath'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> help select-xml
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> Select-Xml -Xml $foo -XPath $Xpath | Select-Object -ExpandProperty Node
Select-Xml: Cannot validate argument on parameter 'Xml'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> Select-Xml -Xml $foo                                                   
Select-Xml: Cannot validate argument on parameter 'Xml'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> $xml.SelectNodes(“//author”) 
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> $xml.SelectNodes(“//book”)  
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> $xml.SelectNodes(“/”)     

xml                            Objects
---                            -------
version="1.0" encoding="utf-8" Objects

PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> $xml.SelectNodes(“/”) | Get-ChildItem
Get-ChildItem: Cannot find path '/home/nicholas/flwor/System.Xml.XmlDocument' because it does not exist.
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> $xml.SelectNodes(“/”) | Get-Content  
Get-Content: The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> 

but how can I "run" an xpath against the xml file? I don't see any examples which have used convertto-xml so perhaps that's the problem.

I've also tried:

PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> $filePath=./bookstore.xml
PS /home/nicholas/flwor> 
PS /home/nicholas/flwor> $node = Select-Xml -Path $filePath -XPath "/" 
Select-Xml: Cannot validate argument on parameter 'Path'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
PS /home/nicholas/flwor> 

where the $filePath variable certainly isn't null or empty. In fact, simply assigning that variable brings up the xml document in gedit.

See also:

https://www.red-gate.com/simple-talk/sysadmin/powershell/powershell-data-basics-xml/

https://dotnet-helpers.com/powershell/reading-xml-files-with-powershell/

https://petri.com/search-xml-files-powershell-using-select-xml

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-xml?view=powershell-7.1

https://stackoverflow.com/a/18509715/4531180

来源:https://stackoverflow.com/questions/65233934/how-to-print-xml-documents-to-the-repl-console-when-using-convertto-xml-in-power

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!