If an XML document includes a namespace, then queries for elements within the document are more difficult. Not only must the namespace tag be included, but an XmlNamespaceManager must be defined.
Select-Xml builds a namespace manager based on the content of a hashtable when the Namespace parameter is used:
$xml = @" <?xml version="1.0"?> <cars xmlns:c="http://example/cars"> <car type="Saloon"> <c:colour>Green</c:colour> <c:doors>4</c:doors> <c:transmission>Automatic</c:transmission> <c:engine> <size>2.0</size> <cylinders>4</cylinders> </c:engine> </car> </cars> "@ Select-Xml '//car/c:engine' -Namespace @{c='http://example/cars'} -Xml $xml
If the SelectNodes method is being used, the XmlNamespaceManager must be built and then passed as an argument:
$namespaceManager = New-Object System.Xml.XmlNamespaceManager($xml.NameTable) $namespaceManager.AddNamespace('c', 'http://example/cars') $xml.SelectNodes( '//car[c:colour="Green"]/c:engine', $namespaceManager )
XML documents, such as group policy reports, are difficult to work with as they often contain many different namespaces. Each of the possible namespaces must be added to a namespace manager.