使用 PowerShell,如何添加多个命名空间(其中一个是默认命名空间)? [英] Using PowerShell, how do I add multiple namespaces (one of which is the default namespace)?

查看:19
本文介绍了使用 PowerShell,如何添加多个命名空间(其中一个是默认命名空间)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个命名空间(默认"命名空间和 xlink)的 XML 文档:

I have an XML document that contains two namespaces (the 'default' namespace and xlink):

  • xmlns="http://embassy/schemas/dudezilla/"
  • xmlns:xlink="http://www.w3.org/1999/xlink"

如何在我的 PowerShell 代码中指定两个"命名空间?PowerShell 似乎需要默认命名空间的前缀.我该怎么做?

How do I specify "both" namespaces in my PowerShell code? PowerShell seems to want a prefix for the default namespace. How do I do this?

现在我有以下代码(不确定默认命名空间要包含什么):

Right now I have the following code (not sure what to include for the default namespace):

    [System.Xml.XmlNamespaceManager] $nsmgr = $xml.NameTable;
    $nsmgr.AddNamespace('?','http://embassy/schemas/dudezilla/');
    [System.Xml.XmlNamespaceManager] $nsmgr = $xml.NameTable;
    $nsmgr.AddNamespace('xlink','http://www.w3.org/1999/xlink');

    [System.Xml.XmlNodeList] $nodelist;
    [System.Xml.XmlElement] $root = $xml.DocumentElement;
    $nodelist = $root.SelectNodes("//image/@xlink:href", $nsmgr);

    Foreach ($xmlnode in $nodelist)
    {
        $xmlnode.Value;
    }

谢谢!

推荐答案

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
}

这篇关于使用 PowerShell,如何添加多个命名空间(其中一个是默认命名空间)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆