Skip to content

alwaysCreateTextNode does not add #text to empty/self-closing tags that have attributes with ignoreAttributes set to false #811

@loucadufault

Description

@loucadufault
  • 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

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?

  • Yes
  • No

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions