How to access element with xpath with namespace in powershell?

Powershell:

$doc = new-object System.Xml.XmlDocument $doc.Load($filename) $items = Select-Xml -Xml $doc -XPath '//item' $items | foreach { $item = $_ write-host $item.name } 

I don't get a way out

XML:

 <?xml version="1.0" encoding="UTF-8"?> <submission version="2.0" type="TREE" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:noNamespaceSchemaLocation="TREE.xsd" xmlns="some/kind/of/tree/v1"> <group> <item></item> <item></item> <item></item> </group> <submission> 
+6
source share
1 answer

You have a few issues. First you need to specify the namespace in the XPath template, the XML is not well formed (the closing tag is not the end tag), and Select-Xml returns XmlInfo, not XmlElement directly. Try the following:

 $xml = [xml]@' <submission version="2.0" type="TREE" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:noNamespaceSchemaLocation="TREE.xsd" xmlns="some/kind/of/tree/v1"> <group> <item></item> <item></item> <item></item> </group> </submission> '@ $ns = @{dns="some/kind/of/tree/v1"} $items = Select-Xml -Xml $xml -XPath '//dns:item' -Namespace $ns $items | Foreach {$_.Node.Name} 
+10
source

Source: https://habr.com/ru/post/955729/


All Articles