Remove unwanted (empty) xmlns attribute added by appendChild

后端 未结 6 1550
失恋的感觉
失恋的感觉 2020-12-18 18:32

I have this code:

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

  $project = $root.Project;

  $beforeBuild = $root.CreateE         


        
相关标签:
6条回答
  • 2020-12-18 18:51

    Check these for possible solutions:

    Powershell and csproj

    Xml namespace and C# csproj

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

    $content = [xml] $content.OuterXml.Replace(" xmlns=`"`"", "")
    $content.Save($_.FullName);
    
    0 讨论(0)
  • 2020-12-18 19:00

    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.

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

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

    0 讨论(0)
  • 2020-12-18 19:02

    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.

    0 讨论(0)
  • 2020-12-18 19:08

    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.

    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.

    0 讨论(0)
  • 2020-12-18 19:10

    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);
    }
    
    0 讨论(0)
  • 2020-12-18 19:11

    Using 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).

    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'

    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:

    <?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 nameSpace variable with a different namespace uri string or another set variable.

    0 讨论(0)
提交回复
热议问题