Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove unwanted (empty) xmlns attribute added by appendChild

Tags:

I have this code:

function setupProject($projectFile) {
  [xml]$root = Get-Content $projectFile;

  $project = $root.Project;

  $beforeBuild = $root.CreateElement("Target", "");
  $beforeBuild.SetAttribute("name", "BeforeBuild");
  $beforeBuild.RemoveAttribute("xmlns");
  $project.AppendChild($beforeBuild);

  $root.Save($projectFile);
}

It should add a new <Target name="BeforeBuild" /> to the XML document.

But it also adds an empty xmlns="" attribute which I don't want. (It's actually Visual Studio which doesn't like this attribute!)

<Target name="BeforeBuild" xmlns="" />

I've already tried this code:

$beforeBuild.RemoveAttribute("xmlns");
$project.AppendChild($beforeBuild);
$beforeBuild.RemoveAttribute("xmlns");
like image 950
ComFreek Avatar asked Nov 04 '12 15:11

ComFreek


People also ask

How do I get rid of xmlns?

Open the map in the Design Studio. Edit the output card in question (in this case output card 1 of the validationMap map) and expand it so you can right click on Property > Schema > Type > Metadata > Name Spaces > http://www.example.com/IPO. After right clicking on http://www.example.com/IPO, left click on Delete.

Why is xmlns added?

You declare default namespace (namespace without prefix) for parent and by default all children should belong to the same namespace. If they don't, xmlns="" is generated to show that they don't belong to parent's namespace.

What is the xmlns attribute?

the xmlns attribute specifies the xml namespace for a document. This basically helps to avoid namespace conflicts between different xml documents, if for instance a developer mixes xml documents from different xml applications.

Why we need xmlns in XML?

One of the primary motivations for defining an XML namespace is to avoid naming conflicts when using and re-using multiple vocabularies. XML Schema is used to create a vocabulary for an XML instance, and uses namespaces heavily.


2 Answers

The xmlns="" namespace (un)declaration has been added because your parent element is in a namespace and your child element is not.

If you don't want this namespace declaration added, the implication is that you want the child element to be in the same namespace as its parent, and the answer is to put it in this namespace at the time you create the element. That is, change the call CreateElement("Target", "") to specify the correct namespace.

like image 93
Michael Kay Avatar answered Sep 29 '22 09:09

Michael Kay


As answered by Michael Kay, the best way to remove this unwanted namespace is creating the new child element in the same namespace as its parent:

function setupProject($projectFile) {
  [xml]$root = Get-Content $projectFile;

  $project = $root.Project;

  # UPDATE THIS LINE $beforeBuild = $root.CreateElement("Target", "");
  $beforeBuild = $root.CreateElement("Target", $project.NamespaceURI);
  $beforeBuild.SetAttribute("name", "BeforeBuild");
  $beforeBuild.RemoveAttribute("xmlns");
  $project.AppendChild($beforeBuild);

  $root.Save($projectFile);
}
like image 38
emekm Avatar answered Sep 29 '22 11:09

emekm