fast-xml-parser icon indicating copy to clipboard operation
fast-xml-parser copied to clipboard

Numeric character references (A, A) are not decoded unless htmlEntities is enabled

Open philppplik opened this issue 1 month ago • 3 comments

With default options, processEntities is true and the five predefined XML entities (<, >, &, ", ') are decoded as expected. Numeric character references, however, are silently left as literal text unless htmlEntities: true is passed:

const { XMLParser } = require('fast-xml-parser');

const parser = new XMLParser(); // processEntities defaults to true

parser.parse('<r>&lt;&gt;&amp;</r>');
// { r: '<>&' }  -- named entities decoded

parser.parse('<r>&#65;&#x42;</r>');
// { r: '&#65;&#x42;' }  -- numeric refs NOT decoded (expected: { r: 'AB' })

parser.parse('<r a="&#65;"/>', { ignoreAttributes: false });
// attributes affected the same way

new XMLParser({ htmlEntities: true }).parse('<r>&#65;&#x42;</r>');
// { r: 'AB' }  -- only decoded when htmlEntities is on

Numeric character references are not HTML entities - they are part of core XML. Per the XML 1.0 spec (section 4.1, "Character and Entity References"), a well-formed XML processor must recognize &#...; and &#x...; references in content and attribute values, independent of any HTML entity table.

Looking at the source, this seems to come from OrderedObjParser wiring numericAllowed to the htmlEntities option when constructing the EntityDecoder:

this.entityDecoder = new EntityDecoder({
  namedEntities: { ...namedEntities, ...externalEntities },
  numericAllowed: this.options.htmlEntities, // <-- ties XML char refs to the HTML entities option
  ...
});

So a user who parses XML with default options (and htmlEntities defaults to false) gets raw &#65; sequences in their data, while named XML entities are decoded. I ran into this with a feed that encodes non-ASCII characters as numeric refs.

Tested on fast-xml-parser 5.11.2 (npm latest) with Node 22. The same behavior exists in 4.x, so it may be intentional - but if so, it would be great to have it documented next to processEntities, since the current default contradicts XML spec compliance.

philppplik avatar Oct 18 '26 04:10 philppplik

We're glad you find this project helpful. We'll try to address this issue ASAP. You can join Discord channel(https://discord.gg/X4Qp9u6Vz) for anonucements and pre release discussions. Don't forget to star this repo.

github-actions[bot] avatar Oct 18 '26 04:10 github-actions[bot]

I am working on the numeric-reference decoding fix and regression coverage for text, attributes, HTML-entity opt-in, and disabled entity processing. AI assistance: OpenClaw (cpa/gpt-6.1-sol); no human review is claimed.

guocn avatar Oct 22 '26 08:10 guocn

enabling numeric entities in this version will be a breaking change for the project already using the library. So you would have to override the configuration.

amitguptagwl avatar Oct 27 '26 04:10 amitguptagwl