xml 删除由 appendChild 添加的不需要的(空的)xmlns 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13220095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:45:11  来源:igfitidea点击:

Remove unwanted (empty) xmlns attribute added by appendChild

xmldompowershellappendchild

提问by ComFreek

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 shouldadd a new <Target name="BeforeBuild" />to the XML document.

应该<Target name="BeforeBuild" />向 XML 文档添加一个新的。

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

但它也添加了一个xmlns=""我不想要的空属性。(实际上是 Visual Studio 不喜欢这个属性!)

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

I've already tried this code:

我已经试过这个代码:

$beforeBuild.RemoveAttribute("xmlns");
$project.AppendChild($beforeBuild);
$beforeBuild.RemoveAttribute("xmlns");

采纳答案by emekm

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:

正如Michael Kay所回答的,删除这个不需要的命名空间的最好方法是在与其父元素相同的命名空间中创建新的子元素:

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

回答by Michael Kay

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.

已添加 xmlns="" 命名空间 (un) 声明,因为您的父元素在命名空间中而您的子元素不在。如果您不想添加此命名空间声明,则意味着您希望子元素与其父元素位于同一命名空间中,答案是在创建元素时将其放入此命名空间。即,更改调用 CreateElement("Target", "") 以指定正确的命名空间。

回答by Neolisk

Check these for possible solutions:

检查这些以获取可能的解决方案:

Powershell and csproj

Powershell 和 csproj

Xml namespace and C# csproj

Xml 命名空间和 C# csproj

Here is a workaround from the second solution that worked for OP:

这是适用于 OP 的第二个解决方案的解决方法:

$content = [xml] $content.OuterXml.Replace(" xmlns=`"`"", "")
$content.Save($_.FullName);

回答by NSTuttle

Using Javascript

使用 JavaScript

If you are using JS to create an XML doc and are getting blank xmlns attributes on child nodes after declaring xmlns="XXXX"on the parent node, use JS createElementNS(namespace, nodeName)instead of createElement(nodeName).

如果您使用 JS 创建 XML 文档并且xmlns="XXXX"在父节点上声明后在子节点上获得空白 xmlns 属性,请使用 JScreateElementNS(namespace, nodeName)而不是createElement(nodeName).

This is assuming you want your child nodes to share the same namespace as the parent. In the below case 'v1','v2', etc. will share the NS of 'data'

这是假设您希望子节点与父节点共享相同的命名空间。在下面的情况下,'v1'、'v2' 等将共享 'data' 的 NS

It would look something like this:

它看起来像这样:

let data = someArray;
let nameSpace = 'XXX';
let doc = "<?xml version='1.0' encoding='utf-8' ?><data xmlns='XXXX'></data>";
let parser = new DOMParser();
let xml = parser.parseFromString(doc, "text/xml");

for (let i = 0; i < data.length; i++) {
    let node = xml.createElementNS(nameSpace , "v" + (i + 1));
    $(node).text(data[i]);
    let elements = xml.getElementsByTagName("data");
    elements[0].appendChild(node);
 }

CORRECT result would look like:

正确的结果如下:

<?xml version='1.0' encoding='utf-8' ?>
<data xmlns='XXXX'>
    <v1></v1>
    <v2></v2>
</data>

Versus BAD result:

与 BAD 结果对比:

<?xml version='1.0' encoding='utf-8' ?>
<data xmlns='XXXX'>
    <v1 xmlns=""></v1>
    <v2 xmlns=""></v2>
</data>

With this solution, you could also declare separate Namespaces for your child nodes. Simply replace the nameSpacevariable with a different namespace uri string or another set variable.

使用此解决方案,您还可以为子节点声明单独的命名空间。只需将nameSpace变量替换为不同的命名空间 uri 字符串或另一个设置变量。

回答by zahid

The namespace is an inherent part of the name of each node.Removing namespace means need to recreate the node again. Here is the code, where you can create the child node without the Namespace property.

命名空间是每个节点名称的固有部分。删除命名空间意味着需要重新创建节点。这是代码,您可以在其中创建没有 Namespace 属性的子节点。

That means that if your main tag contains namespace attribute and your child doesn't have.Hence, the child node will inherit the defaulted namespace attribute from the parent. The best way to remove the namespace attribute is

这意味着如果你的主标签包含命名空间属性而你的孩子没有。因此,子节点将从父节点继承默认的命名空间属性。删除命名空间属性的最佳方法是

[xml]$oXMLDocument = (Get-Content "D:\myXml.xml")
# Assuming Project is the parent node with a namespace
$project = $oXMLDocument.Project
$childNode = $oXMLDocument.CreateElement("test",$project.NamespaceURI)
# (Optional) Add any attributes to the element
$childNode.SetAttribute("name", "value")
$oXMLDocument.DocumentElement.AppendChild($childNode)
# Save the document
$oXMLDocument.Save("D:\myXml2.xml")

Basically, this will not remove the namespace attribute from the child note. And in fact you can't.This will hide the attribute as defaulted

基本上,这不会从子笔记中删除命名空间属性。事实上你不能。这将隐藏默认的属性

If you need to create the subchild under your child node, then follow the same style.

如果您需要在您的子节点下创建子子节点,则遵循相同的样式。

回答by zahid

The namespace is an inherent part of the name of each node.Removing namespace means need to recreate the node again. Here is the code, where you can create the child node without the Namespace property.

命名空间是每个节点名称的固有部分。删除命名空间意味着需要重新创建节点。这是代码,您可以在其中创建没有 Namespace 属性的子节点。

[xml]$oXmlDocume = [xml] (Get-Content  D:\myXml.xml)
// Assuming Project is the parent node
$project = $oXMLDocument.Project
$childNode = $oXMLDocument.CreateElement("Child",$project.NamespaceURI)
##代码##XMLDocument.AppendChild($ChildNode)

If you need to create the subchild under your child node, then follow the same style.

如果您需要在您的子节点下创建子子节点,则遵循相同的样式。