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");
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.
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.
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.
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.
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With