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

XMLBuilder: Attributes with undefined values produce odd whitespace when used with format: true #585

Closed
5 of 6 tasks
ChrisMBarr opened this issue Jun 7, 2023 · 3 comments
Labels
Pending Pending to be confirmed by user/author for some check/update/implementation

Comments

@ChrisMBarr
Copy link
Contributor

  • Are you running the latest version?
  • Have you included sample input, output, error, and expected output?
  • Have you checked if you are using correct configuration?
  • Did you try online tool?

Description

Using XMLBuilder when the format: true option is on and an array contains a property with undefined as the value it produces odd whitespace formatting. The text is not on the same line as the opening XML tag, and there are several spaces added before the closing tag.

The XML output is correct in how it creates the tags and their attributes, but the whitespace is just all wrong.

Code

const xmlObj = {
  "list": {
	"item":[
	  "one",
	  {"#text": "two"},
	  {"#text": "three", "@_attr": undefined},
	  {"#text": "two",   "@_attr": "foo"}
	]
  }
};
const opts = {
  ignoreAttributes: false,
  format: true
};
const builder = new fxp.XMLBuilder(opts);
console.log(builder.build(xmlObj));

Actual Output

<list>
  <item>one</item>
  <item>two</item>
  <item>
three  </item>
  <item attr="foo">two</item>
</list>

Expected Output

<list>
  <item>one</item>
  <item>two</item>
  <item>three</item>
  <item attr="foo">two</item>
</list>

What I am working on has optional object properties, so I just pass the data to the property as is. Here is my real use case in my TypeScript project

interface IObj {
  value: string;
  part?: string;
}

function getXmlNodes(dataArr: IObj[]){
  const xmlObjs = [];
  
  for(let item of dataArr){
    xmlObjs.push({
      '#text': item.value,
      '@_part': item.part,
    });
  }

  return xmlObjs;
}

Would you like to work on this issue?

  • Yes
  • No
@github-actions
Copy link

github-actions bot commented Jun 7, 2023

We're glad you find this project helpful. We'll try to address this issue ASAP. You can vist https://solothought.com to know recent features. Don't forget to star this repo.

@amitguptagwl amitguptagwl added the Pending Pending to be confirmed by user/author for some check/update/implementation label Jun 8, 2023
@cecia234
Copy link
Contributor

cecia234 commented Jul 19, 2023

Hi @amitguptagwl, I found the same odd behaviour. Moreover, there is another "odd" case with formatting enabled when attributes are specified as null.

Using the same structure as the one of @FiniteLooper, with the starting object as follows

const xmlObj = {
  "list": {
	"item":[
	  "one",
	  {"#text": "two"},
	  {"#text": "three", "@_attr": null},
	  {"#text": "two",   "@_attr": "foo"}
	]
  }
};

the final output is

<list>
  <item>one</item>
  <item>
three    <@_attr/>
  </item>
  <item attr="foo">two</item>
</list>

If this and the former are unexpected behaviours, I could fix them! I already looked at the code and it shouldn't be that hard.

In that case, I think that the expected output should also be discussed (empty string in the attribute value, no attribute at all, etc.)

@amitguptagwl
Copy link
Member

Hi @cecia234, since null and undefined doesn't represent any value, it's better if we skip that attribute from the output.

cecia234 added a commit to cecia234/fast-xml-parser that referenced this issue Jul 22, 2023
cecia234 added a commit to cecia234/fast-xml-parser that referenced this issue Jul 22, 2023
cecia234 added a commit to cecia234/fast-xml-parser that referenced this issue Jul 22, 2023
cecia234 added a commit to cecia234/fast-xml-parser that referenced this issue Jul 22, 2023
NaturalIntelligence#585)

Null and Undefined attributes should be suppressed when ignoreAttributes is false
both when format is true and when it is false
amitguptagwl pushed a commit that referenced this issue Jul 30, 2023
* fix (xmlbuilder): null and undefined attributes are suppressed in the built xml (#585)

* test (xmlbuilder): add tests on null and undefined attributes building (#585)

Null and Undefined attributes should be suppressed when ignoreAttributes is false
both when format is true and when it is false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Pending Pending to be confirmed by user/author for some check/update/implementation
Projects
None yet
Development

No branches or pull requests

3 participants