1

I have the following code which correctly fetches the AD Property links from the MSDN documentation:

$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)

Originally I'd hoped to avoid adding the namespace prefix (ns:), which the documentation implies can be done by adding a namespace with the prefix string.Empty. This seems to work in terms of setting the default namespace; but SelectNodes does not make use of this default.

$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)

Question:

Is there any way to have PowerShell use SelectNodes without requiring a namespace prefix / via setting a default namespace?

1

1 Answer 1

3

From what I can tell, the concept of a default namespace in the sense of not having to prefix node names with it:

  • applies to XML documents

  • does not apply to XPath expressions.

In other words:

  • If a document / element subtree declares a default namespaces along the lines of <foo xmlns='http://example.org'>, that element and all descendants not using a namespace prefix are implicitly in that default namespace.

  • By contrast, referring to such nodes in the context of an XPath expression requires that you:

    • choose a prefix to map the document's default namespace URI to (ns in your example)
    • explicitly use that prefix to match nodes in the default namespace (e.g., ns:div)

The above is supported by the following excerpt from the documentation (emphasis added):

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.