Attribute values are not normalized as XML 1.0 section 3.3.3 requires

Open
#920 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 4 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
javascript
Domain
backend

Research direction

Start by reading the XMLParser attribute handling and entity decoding paths, then check issue #330 for the existing behavior around preserving newlines. The report says normalization must happen before entity and character references are decoded and may need to be opt-in. Done means literal attribute whitespace normalizes to spaces while a decoded character reference preserves its newline; add coverage for both forms.

Written by the indexing model from the issue text.

Description

  • 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

XMLParser does not apply the attribute-value normalization of XML 1.0, section 3.3.3. A literal line feed or tab in an attribute value is kept as is, but the spec says that a conforming processor replaces each literal whitespace character (#x20, #xD, #xA, #x9) with a space, and only a character reference such as 
 adds the character itself.

Because the parser keeps the literal characters, the two forms cannot be told apart once character references are decoded (htmlEntities: true or an entityDecoder): a="x
y" and an attribute with a real line break both become "x\ny". An application that writes the value back has no way to know whether the source meant a space or a line feed.

I found no option for this. #330 asked for the opposite (keeping newlines) in 2021, and that is the current behavior, so a change would probably have to be opt-in, for example an option such as normalizeAttributeValues: true. The normalization would have to happen before entity and character references are decoded.

Input
<r a="x
y"/>
Code
import { XMLParser } from "fast-xml-parser"; // 5.12.0

const parser = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: "" });

parser.parse('<r a="x\ny"/>').r.a;      // "x\ny"
parser.parse('<r a="x\ty"/>').r.a;      // "x\ty"
parser.parse('<r a="x\r\ny"/>').r.a;    // "x\ny"
parser.parse('<r a="x&#10;y"/>').r.a;   // "x&#10;y" (numeric references are not decoded by default, see #892)
Output
"x\ny"
"x\ty"
"x\ny"
expected data
"x y"
"x y"
"x y"
"x\ny"   // for a="x&#10;y", once numeric references are decoded

For comparison, @xmldom/xmldom returns "x y" for the first two inputs and "x\ny" for a="x&#10;y".

Would you like to work on this issue?

  • Yes
  • No
Dominant language
JavaScript
Stars
3.1k
Forks
410
Avg merge
4d 15h
Merged PRs (30d)
8

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from NaturalIntelligence/fast-xml-parser

All issues in NaturalIntelligence/fast-xml-parser

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.