Skip to content

Issue: xsi:nil="true" not serialized correctly in output XML #749

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

Closed
6 of 7 tasks
irisfield opened this issue May 12, 2025 · 2 comments
Closed
6 of 7 tasks

Issue: xsi:nil="true" not serialized correctly in output XML #749

irisfield opened this issue May 12, 2025 · 2 comments

Comments

@irisfield
Copy link

  • 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?
  • Have you checked the docs for helpful APIs and examples?

Description

This is related to #123. I am also trying to serialize JSON to XML to make requests to a SOAP API. I need a way to add the attribute xsi:nil="true" to the serialized XML. I am getting an unexpected output.

Input

const xmlBuilder = new XMLBuilder({
  attributeNamePrefix: '@',
  ignoreAttributes: false,
  suppressEmptyNode: true,
  format: true,
});

const output = xmlBuilder.build({
  "Family": "",
  "Genus": "Iris",
  "Subgenus": { "@xsi:nil": "true" },
  "Species": "Bracteata",
})

Output

<Family/>
<Genus>Iris</Genus>
<Subgenus xsi:nil/>
<Species>Bracteata</Species>

expected data

<Family/>
<Genus>Iris</Genus>
<Subgenus xsi:nil="true"/>
<Species>Bracteata</Species>

Observation

When I set "@xsi:nil" to anything but "true" or true it works as expected. For example:

const output = xmlBuilder.build({
  "Family": "",
  "Genus": "Iris",
  "Subgenus": { "@xsi:nil": "false" },
  "Species": "Bracteata",
})

outputs:

<Family/>
<Genus>Iris</Genus>
<Subgenus xsi:nil="false"/>
<Species>Bracteata</Species>

Would you like to work on this issue?

  • Yes
  • No

Bookmark this repository for further updates. Visit SoloThought to know about recent features.

Copy link

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.

@irisfield
Copy link
Author

irisfield commented May 12, 2025

I found the solution. The suppressBooleanAttributes option must be set to false:

Input

const xmlBuilder = new XMLBuilder({
  attributeNamePrefix: '@',
  ignoreAttributes: false,
  suppressEmptyNode: true,
  suppressBooleanAttributes: false,
  format: true,
});

const output = xmlBuilder.build({
  "Family": "",
  "Genus": "Iris",
  "Subgenus": { "@xsi:nil": "true" },
  "Species": "Bracteata",
})

Output

<Family/>
<Genus>Iris</Genus>
<Subgenus xsi:nil="true"/>
<Species>Bracteata</Species>

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

No branches or pull requests

1 participant