且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 PowerShell,如何添加多个命名空间(其中一个是默认命名空间)?

更新时间:2023-11-24 16:18:28

PowerShell v2 让这更简单:

PowerShell v2 makes this simpler:

$ns = @{
         dns="http://embassy/schemas/dudezilla/"
         xlink="http://www.w3.org/1999/xlink"
       }

$xml | Select-Xml '//dns:image/@xlink:href' -Namespace $ns

如果你想换一种方式试试:

If you want to do it the other way try:

$nsmgr = New-Object System.Xml.XmlNamespaceManager $xml.NameTable
$nsmgr.AddNamespace('dns','http://embassy/schemas/dudezilla/')
$nsmgr.AddNamespace('xlink','http://www.w3.org/1999/xlink')

$root = $xml.DocumentElement
$nodelist = $root.SelectNodes("//dns:image/@xlink:href", $nsmgr)

foreach ($xmlnode in $nodelist)
{
    $xmlnode.Value
}