Working with namespaces

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 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 = @" 
<?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, XmlNamespaceManager must be built first and 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.240.142