Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When using namespace, empty XMLNs shows up in various child node elements. #6

Closed
GlassHill opened this issue Mar 5, 2020 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@GlassHill
Copy link

GlassHill commented Mar 5, 2020

Description: When using namespace, empty XMLNs shows up in all child node elements.

If you want to reproduce:

Create a new xml with the root element having a namespace. Once you start appending child nodes those will also have namespaces defined but will be empty, the namespaces for these should not exist.

let xmlDoc = create("UTF-8").ele("TestNameSpace", "test"); xmlDoc.ele({child: "TestTest"}) console.log(xmlDoc.end({ prettyPrint: true }));

output:

<?xml version="1.0" encoding="UTF-8"?>
<test xmlns="TestNameSpace">
    <child xmlns="">TestTest</child>
</test>

Expected Behavior: Child nodes should not have empty XMLNs defined.

Version:

  • node.js: v12.13.0
  • xmlbuilder2: 6.12.0

Note: I could be wrong, perhaps this is intended behavior. If it is, is there something I can do so that child elements that do not need XMLNs won't have an empty field appearing in it?

@GlassHill GlassHill added the bug Something isn't working label Mar 5, 2020
@oozcitak
Copy link
Owner

oozcitak commented Mar 6, 2020

Yes this is by design. Child nodes do not inherit their parent namespace. If the child node in your example didn't have the xmlns="" attribute when serialzed that would mean it inherited the parent node's default namespace declaration. This may or may not be what you want so xmlbuilder opts to be explicit about this. The intented use case is:

const ns = "TestNameSpace";
let xmlDoc = create("UTF-8").ele(ns, "test"); 
xmlDoc.ele(ns, "child").txt("TestTest") ;
console.log(xmlDoc.end({ prettyPrint: true }));

Let me know if that makes. Otherwise I can add a flag to let child nodes inherit their parent namespace recursively.

@GlassHill
Copy link
Author

GlassHill commented Mar 6, 2020

Is it possible to add a flag to allow for default namespaces?

For example the following would mean that all child nodes that fall under it would be for the 'xmlns="fruit"' namespace while the second blocks child nodes falls under vegetable.

<root>
<table xmlns="fruit">
  <tr>
    <td>Apples</td>
    <td>Bananas</td>
  </tr>
</table>

<table xmlns="vegetable">
  <tr>
    <td>carrot</td>
    <td>raddish</td>
  </tr>
</table>
</root>

and if you really wanted to get technical you can also cancel a default namespace by adding a 'cancel operation' if needed: xmlns="".

@oozcitak
Copy link
Owner

oozcitak commented Mar 6, 2020

Sure, will do.

@oozcitak oozcitak added enhancement New feature or request and removed bug Something isn't working labels Mar 6, 2020
@GlassHill
Copy link
Author

Just to add on, instead of using the namespace field, if I used the .att method to create the XMLNS:

xmlDoc.att("xmlns", "testtest");

It results in what I want, where the child nodes don't have an empty xmlns field.

It would be nice to have a flag for namespace regardless however.

@oozcitak
Copy link
Owner

Upon further reflection I decided to remove the inheritNS option, namespaces are now inherited automatically (your sample code in the first post would work). FYI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants