2013-10-11 20 views
6

PowerShell:Come accedere ad un elemento con xpath con uno spazio dei nomi in PowerShell?

$doc = new-object System.Xml.XmlDocument 
$doc.Load($filename) 

$items = Select-Xml -Xml $doc -XPath '//item' 
$items | foreach { 
    $item = $_ 
    write-host $item.name 
} 

ottengo alcun output

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> 

risposta

10

Hai avuto qualche problema in corso. Per prima cosa è necessario specificare lo spazio dei nomi nel modello XPath, l'XML non è ben formato (il tag di chiusura non è un tag di chiusura) e Select-Xml restituisce direttamente XmlInfo e non XmlElement. Prova questo:

$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} 
Problemi correlati