2012-11-06 5 views
1

Pourquoi ne reçois-je aucun résultat en utilisant le code XML et XPath ci-dessous?La requête XPath ne renvoie aucun résultat

C'est en fait PowerShell mais je l'ai aussi essayé avec this online tool mais encore une fois: il ne sort rien!

Je pense que je suis totalement négliger quelque chose!

[xml] $xml = @" 
<?xml version="1.0" encoding="utf-8"?> 
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    <ItemGroup Label="ProjectConfigurations"> 
    <ProjectConfiguration Include="Debug|AnyCPU"> 
     <Configuration>Debug</Configuration> 
     <Platform>AnyCPU</Platform> 
    </ProjectConfiguration> 
    <ProjectConfiguration Include="Debug|ARM"> 
     <Configuration>Debug</Configuration> 
     <Platform>ARM</Platform> 
    </ProjectConfiguration> 
    <ProjectConfiguration Include="Debug|x64"> 
     <Configuration>Debug</Configuration> 
     <Platform>x64</Platform> 
    </ProjectConfiguration> 
    <ProjectConfiguration Include="Debug|x86"> 
     <Configuration>Debug</Configuration> 
     <Platform>x86</Platform> 
    </ProjectConfiguration> 
    <ProjectConfiguration Include="Release|AnyCPU"> 
     <Configuration>Release</Configuration> 
     <Platform>AnyCPU</Platform> 
    </ProjectConfiguration> 
    </ItemGroup> 
</Project> 
"@; 

Select-Xml "//ItemGroup" $xml | % { 
    Write-Host $_.Node.GetType(); 
} 
+2

Vous surplomberez l'espace de noms. – choroba

Répondre

1

C'est à cause de l'espace de noms par défaut.

Essayez //*[local-name()='ItemGroup'].

ou /*/*[local-name()='ItemGroup']

ou /*/*

Tous les trois de ceux-ci devraient revenir ItemGroup.

+0

Merci beaucoup! Vous ne connaissez pas de méthode plus simple dans PowerShell, n'est-ce pas? Y a-t-il des abréviations pour cela? – ComFreek

+0

@ComFreek - Désolé, je ne sais rien à propos de PowerShell. Le seul raccourci est vraiment avec XPath 2.0 et vous pouvez simplement utiliser '*' pour le préfixe. ('*: ItemGroup'). –

+0

PowerShell (en fait .NET) ne prend pas en charge XPath 2.0 :( – ComFreek

0

Si vous aviez un préfixe pour votre espace de noms, vous pouvez également faire:

//prefix:ItemGroup 
+0

Que XML provient d'un fichier de projet VS 2012, je ne peux pas changer les espaces de noms. – ComFreek

Questions connexes