Description
When alwaysCreateTextNode: true and ignoreAttributes: false, empty or self-closing tags without attributes correctly get { "#text": "" }. However, empty or self-closing tags with attributes do not get a #text property — they only contain the attribute keys. This is inconsistent: the alwaysCreateTextNode option should guarantee a #text key regardless of whether the tag has attributes.
This makes it impossible to rely on #text always being present, which defeats the purpose of the option.
Input
<rootNode>
<tag>value</tag>
<tag2 some="attribute">12345</tag2>
<empty></empty>
<empty/>
<emptyWithAttr some="attribute"></emptyWithAttr>
<selfClosingWithAttr some="attribute"/>
</rootNode>
Code
import { XMLParser } from 'fast-xml-parser';
const options = {
alwaysCreateTextNode: true,
ignoreAttributes: false,
parseAttributeValue: true,
};
const parser = new XMLParser(options);
const result = parser.parse(xmlData);
Output
{
"rootNode": {
"tag": { "#text": "value" },
"tag2": { "#text": 12345, "@_some": "attribute" },
"empty": [
{ "#text": "" },
{ "#text": "" }
],
"emptyWithAttr": { "@_some": "attribute" },
"selfClosingWithAttr": { "@_some": "attribute" }
}
}
expected data
{
"rootNode": {
"tag": { "#text": "value" },
"tag2": { "#text": 12345, "@_some": "attribute" },
"empty": [
{ "#text": "" },
{ "#text": "" }
],
"emptyWithAttr": { "#text": "", "@_some": "attribute" },
"selfClosingWithAttr": { "#text": "", "@_some": "attribute" }
}
}
Would you like to work on this issue?
Bookmark this repository for further updates. Visit SoloThought to know about recent features.
Description
When
alwaysCreateTextNode: trueandignoreAttributes: false, empty or self-closing tags without attributes correctly get{ "#text": "" }. However, empty or self-closing tags with attributes do not get a#textproperty — they only contain the attribute keys. This is inconsistent: thealwaysCreateTextNodeoption should guarantee a#textkey regardless of whether the tag has attributes.This makes it impossible to rely on
#textalways being present, which defeats the purpose of the option.Input
Code
Output
{ "rootNode": { "tag": { "#text": "value" }, "tag2": { "#text": 12345, "@_some": "attribute" }, "empty": [ { "#text": "" }, { "#text": "" } ], "emptyWithAttr": { "@_some": "attribute" }, "selfClosingWithAttr": { "@_some": "attribute" } } }expected data
{ "rootNode": { "tag": { "#text": "value" }, "tag2": { "#text": 12345, "@_some": "attribute" }, "empty": [ { "#text": "" }, { "#text": "" } ], "emptyWithAttr": { "#text": "", "@_some": "attribute" }, "selfClosingWithAttr": { "#text": "", "@_some": "attribute" } } }Would you like to work on this issue?
Bookmark this repository for further updates. Visit SoloThought to know about recent features.