Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PowerShell, how do I add multiple namespaces (one of which is the default namespace)?

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"

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;
    }

Thanks!

like image 447
Keith Avatar asked Mar 11 '11 19:03

Keith


People also ask

What is the default namespace?

A default namespace is a namespace that does not include a prefix. The default prefix is applied to all the elements that do not include a prefix and is unique for different XMLports. For example, the following string specifies a namespace: urn:microsoft-dynamics-nav/xmlports/x100 , where 100 is the ID of the XMLport.

What is default namespace explain with example?

All elements and attributes in the document that do not have a prefix will then belong to the default namespace. The following example declares that the <BOOK> element and all elements and attributes within it ( <TITLE> , <PRICE> , currency ) are from the namespace urn:example.microsoft.com:BookInfo . XML Copy.

What is the default namespace The namespace used by default when no namespace is declared?

No namespace exists when there is no default namespace in scope. A {default namespace} is one that is declared explicitly using xmlns.

How do I change the default namespace in XML?

You can change the default namespace within a particular element by adding an xmlns attribute to the element. Example 4-4 is an XML document that initially sets the default namespace to http://www.w3.org/1999/xhtml for all the XHTML elements. This namespace declaration applies within most of the document.


1 Answers

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
}
like image 76
Keith Hill Avatar answered Oct 05 '22 06:10

Keith Hill