Visitar URL original
Don't create text node for self closing tags when `alwaysCreateTextNode: true` · Issue #708 · NaturalIntelligence/fast-xml-parser · GitHub
Skip to content

Don't create text node for self closing tags when alwaysCreateTextNode: true #708

Open

Description

@langonja

Currently I can have the parser create a text node on tags without attributes by setting alwaysCreateTextNode: true. However, for self closing tags, I would like the parser to not create a text node. Right now it creates a text node set to an empty string.

I also noticed that if a self closing tags have any attributes, a text node is not created. It only seems like it's created when a self closing tags have no attributes.

// Given these settings:
const parser = new XMLParser({
	ignoreAttributes: false,
	allowBooleanAttributes: true,
	parseTagValue: false,
	alwaysCreateTextNode: true,
});

parser.parse("<workbookPr defaultThemeVersion="153222" />") 
// "workbookPr": {
//   "@_defaultThemeVersion": "153222"
// }

parser.parse("<workbookPr />") 
// "workbookPr": {
//   "#text": ""
// }

I expect that "#text" is always present, no matter if the self closing tag have attributes or not. But my primary expectation is to be able to have no text nodes at all for self closing tags while still allowing the parser to make text nodes for tags without any attributes.

Activity

  1. github-actions commented on Feb 13, 2025

    @github-actions

    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.

  2. changed the title [-]Can I create a text node if the tag is not self closing?[/-] [+]Creating text nodes for tags without any attributes.[/+] on Feb 13, 2025
  3. changed the title [-]Creating text nodes for tags without any attributes.[/-] [+]Don't create text node for self closing tags when `alwaysCreateTextNode: true`[/+] on Feb 13, 2025
  4. kronodeus commented on May 20, 2025

    @kronodeus

    Just wanted to chime in here: Our project relies on #text always being present. We ran into this same bug where #text is missing for self-closing elements that have attributes. We're not asking for the same feature the OP is asking for, but would love for that bug to be fixed.

  5. hiSandog commented on Jul 8, 2026

    @hiSandog

    The scope would be easier to review if the first version defined a small acceptance contract for alwaysCreateTextNode: true. I would call out the default behavior, the migration/backwards-compatibility story, and one or two cases that are intentionally out of scope so the implementation does not grow beyond the original request.

  6. hengweiyv commented on Oct 4, 2026

    @hengweiyv

    The missing-text-node behavior reported here for self-closing elements with attributes is fixed by commit 31bbc99, which is included in v5.7.3 and later tags.

    I checked current master (80e88a8, package version 5.11.2) with the original workbookPr example plus empty paired tags, boolean attributes, and custom textNodeName, with alwaysCreateTextNode both enabled and disabled: all 16 checks pass. With the option enabled, the attributed self-closing element includes an empty text property; with it disabled, the existing attribute-only output is preserved.

    This addresses the missing #text report, but does not add the separate option requested in the opening post to exclude text nodes specifically for self-closing elements.

  7. yuefdev commented on Oct 17, 2026

    @yuefdev

    I'll add a short documentation example for the current empty-element behaviour with attributes, covering both values of alwaysCreateTextNode. This will clarify the existing behaviour rather than introduce a new parser option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions