在 PowerShell 中为命名空间管理器设置默认命名空间
Setting a default namespace for a namespace manager in PowerShell
我有以下代码可以从 MSDN documentation:
正确获取 AD 属性 链接
$uri = 'https://msdn.microsoft.com/en-us/library/ms675090(v=vs.85).aspx' #lists all AD attributes
$results = [xml](Invoke-RestMethod -Method Get -Uri $uri -UseBasicParsing -UseDefaultCredentials)
[System.Xml.XmlNamespaceManager] $nsMgr = New-Object -TypeName System.Xml.XmlNamespaceManager($results.NameTable)
$nsMgr.AddNamespace('ns','http://www.w3.org/1999/xhtml')
$results.SelectNodes("/ns:html/ns:body/ns:div[@id = 'page']/ns:div[@id = 'body']/ns:div[@id = 'content']/ns:div[@class = 'topic']/ns:div[@id = 'mainSection']/ns:dl/ns:dd/ns:a/@href",$nsMgr)
最初我希望避免添加命名空间前缀 (ns:
),the documentation 暗示可以通过添加带有前缀 string.Empty
的命名空间来完成。这似乎在设置默认名称空间方面起作用;但是 SelectNodes
没有使用这个默认值。
$uri = 'https://msdn.microsoft.com/en-us/library/ms675090(v=vs.85).aspx' #lists all AD attributes
$results = [xml](Invoke-RestMethod -Method Get -Uri $uri -UseBasicParsing -UseDefaultCredentials)
[System.Xml.XmlNamespaceManager] $nsMgr = New-Object -TypeName System.Xml.XmlNamespaceManager($results.NameTable)
#$nsMgr.AddNamespace('ns','http://www.w3.org/1999/xhtml') #tried with and without this line
$nsMgr.AddNamespace([string]::Empty,'http://www.w3.org/1999/xhtml')
$nsMgr.DefaultNamespace #returns http://www.w3.org/1999/xhtml as hoped
$results.SelectNodes("/html",$nsMgr).Name #should return `html` but doesn't (though works if we register the prefix and use /ns:html)
问题:
有什么方法可以让 PowerShell 使用 SelectNodes
而无需命名空间前缀/通过设置默认命名空间?
据我所知,默认命名空间的概念在不必为节点名称添加前缀的意义上:
适用于XML 文档
不是否适用于XPath 表达式。
换句话说:
如果文档/元素子树按照 <foo xmlns='http://example.org'>
行声明默认名称空间,则该元素和所有未使用名称空间前缀的后代 隐式 在该默认命名空间中。
相比之下,在 XPath 表达式 的上下文中引用此类节点需要您:
- 选择 一个前缀以将文档的默认命名空间 URI 映射到(
ns
在您的示例中)
- 明确使用该前缀来匹配默认命名空间中的节点(例如,
ns:div
)
以上内容得到以下摘自 the documentation 的支持(强调已添加):
If the XPath expression does not include a prefix, it is assumed that the namespace Uniform Resource Identifier (URI) is the empty namespace. If your XML includes a default namespace, you must still add a prefix and namespace URI to the XmlNamespaceManager; otherwise, no nodes will be selected.
我有以下代码可以从 MSDN documentation:
正确获取 AD 属性 链接$uri = 'https://msdn.microsoft.com/en-us/library/ms675090(v=vs.85).aspx' #lists all AD attributes
$results = [xml](Invoke-RestMethod -Method Get -Uri $uri -UseBasicParsing -UseDefaultCredentials)
[System.Xml.XmlNamespaceManager] $nsMgr = New-Object -TypeName System.Xml.XmlNamespaceManager($results.NameTable)
$nsMgr.AddNamespace('ns','http://www.w3.org/1999/xhtml')
$results.SelectNodes("/ns:html/ns:body/ns:div[@id = 'page']/ns:div[@id = 'body']/ns:div[@id = 'content']/ns:div[@class = 'topic']/ns:div[@id = 'mainSection']/ns:dl/ns:dd/ns:a/@href",$nsMgr)
最初我希望避免添加命名空间前缀 (ns:
),the documentation 暗示可以通过添加带有前缀 string.Empty
的命名空间来完成。这似乎在设置默认名称空间方面起作用;但是 SelectNodes
没有使用这个默认值。
$uri = 'https://msdn.microsoft.com/en-us/library/ms675090(v=vs.85).aspx' #lists all AD attributes
$results = [xml](Invoke-RestMethod -Method Get -Uri $uri -UseBasicParsing -UseDefaultCredentials)
[System.Xml.XmlNamespaceManager] $nsMgr = New-Object -TypeName System.Xml.XmlNamespaceManager($results.NameTable)
#$nsMgr.AddNamespace('ns','http://www.w3.org/1999/xhtml') #tried with and without this line
$nsMgr.AddNamespace([string]::Empty,'http://www.w3.org/1999/xhtml')
$nsMgr.DefaultNamespace #returns http://www.w3.org/1999/xhtml as hoped
$results.SelectNodes("/html",$nsMgr).Name #should return `html` but doesn't (though works if we register the prefix and use /ns:html)
问题:
有什么方法可以让 PowerShell 使用 SelectNodes
而无需命名空间前缀/通过设置默认命名空间?
据我所知,默认命名空间的概念在不必为节点名称添加前缀的意义上:
适用于XML 文档
不是否适用于XPath 表达式。
换句话说:
如果文档/元素子树按照
<foo xmlns='http://example.org'>
行声明默认名称空间,则该元素和所有未使用名称空间前缀的后代 隐式 在该默认命名空间中。相比之下,在 XPath 表达式 的上下文中引用此类节点需要您:
- 选择 一个前缀以将文档的默认命名空间 URI 映射到(
ns
在您的示例中) - 明确使用该前缀来匹配默认命名空间中的节点(例如,
ns:div
)
- 选择 一个前缀以将文档的默认命名空间 URI 映射到(
以上内容得到以下摘自 the documentation 的支持(强调已添加):
If the XPath expression does not include a prefix, it is assumed that the namespace Uniform Resource Identifier (URI) is the empty namespace. If your XML includes a default namespace, you must still add a prefix and namespace URI to the XmlNamespaceManager; otherwise, no nodes will be selected.