xmlbuilder2
xmlbuilder2 copied to clipboard
Spaces getting lost after xml tags
Describe the bug
const { fragment } = require('xmlbuilder2');
const mainTag = 'permissions';
const root = fragment({ encoding: 'UTF-8' }).ele(mainTag);
let content = '<copyright-statement>this copyright <bold>statement</bold> <italic>here</italic> <ext-link xmlns:xlink="http://www.w3.org/1999/xlink" ext-link-type="uri" xlink:href="google.com">wit</ext-link></copyright-statement><license xmlns:xlink="http://www.w3.org/1999/xlink" ><license-p>some license statement here <bold>statement</bold> <italic>here </italic> <ext-link xmlns:xlink="http://www.w3.org/1999/xlink" ext-link-type="uri" xlink:href="google.com">wit</ext-link>h stuff </license-p></license>';
console.log(content)
content = root.ele(content).end()
console.log(content)
To Reproduce The above code should suffice to reproduce the issue.
Expected behavior Space should be preserved after tags.
Version:
- node.js: [v17.6.0]
- xmlbuilder2 [3.0.2]
Additional context Add any other context about the problem here.
tried to come up with workaround using custom parser but not able to make it work completely. The content is getting XML encoded
const { fragment } = require('xmlbuilder2');
const mainTag = 'permissions';
let root = fragment({
encoding: 'UTF-8',
parser: { parse: (parent, contents) => {
return parent.txt(contents)
} },
}).ele(mainTag)
let content = '<copyright-statement>this copyright <bold>statement</bold> <italic>here</italic> <ext-link xmlns:xlink="http://www.w3.org/1999/xlink" ext-link-type="uri" xlink:href="google.com">wit</ext-link></copyright-statement><license xmlns:xlink="http://www.w3.org/1999/xlink" ><license-p>some license statement here <bold>statement</bold> <italic>here </italic> <ext-link xmlns:xlink="http://www.w3.org/1999/xlink" ext-link-type="uri" xlink:href="google.com">wit</ext-link>h stuff </license-p></license>';
console.log(content)
content = root.ele(content).end()
console.log(content)
I've got what I believe is a similar issue, but in this case elements that contain only a space are having the space removed after being converted to an object and back. To replicate:
import { convert } from 'xmlbuilder2';
const xml = `<t> </t>`;
const obj = convert(xml, { format: 'object' });
const newXml = convert({ encoding: 'UTF-8' }, obj);
console.log(newXml);
This results in <?xml version="1.0" encoding="UTF-8"?><t/>
After examining the code, I discovered that I change the initial convert line to solve the problem:
const obj = convert({skipWhitespaceOnlyText: false}, xml, { format: 'object' });