Repository navigation
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
When a processing instruction contains a single ' or " (free text such as <?note don't forget?>), XMLParser does not stop at the PI's ?>. It skips to the next matching quote later in the document and then to the next ?>, so everything in between is silently missing from the result. With no later quote it throws Pi Tag is not closed. XMLValidator accepts both documents. Per XML 1.0 production [16] a PI ends at the first ?>.
Tested with 5.12.1 (master at d92f44f) on Node v26.13.0, default options. 5.11.1 and 4.5.7 behave the same.
Input
<r><?note don't forget?><a>1</a><b>it's</b><?end?><c>3</c></r><r><?note don't forget?><a>1</a></r>Code
import { XMLParser, XMLValidator } from 'fast-xml-parser';
const parser = new XMLParser();
const inputs = [
`<r><?note don't forget?><a>1</a><b>it's</b><?end?><c>3</c></r>`,
`<r><?note don't forget?><a>1</a></r>`,
];
for (const xml of inputs) {
console.log('input: ', xml);
console.log('validate:', XMLValidator.validate(xml));
try {
console.log('parse: ', JSON.stringify(parser.parse(xml)));
} catch (err) {
console.log('parse: throws', err.message);
}
}Output
input: <r><?note don't forget?><a>1</a><b>it's</b><?end?><c>3</c></r>
validate: true
parse: {"r":{"?note":"","c":3}}
input: <r><?note don't forget?><a>1</a></r>
validate: true
parse: throws Pi Tag is not closed.
expected data
{"r":{"?note":"","a":1,"b":"it's","?end":"","c":3}}
{"r":{"?note":"","a":1}}
Cause and suggested fix
PIs are read with the attribute-aware tag scanner, readTagExp(xmlData, i, false, "?>"):
fast-xml-parser/src/xmlparser/OrderedObjParser.js
Lines 361 to 364 in d92f44f
| } else if (c1 === 63) { //'?' | |
| let tagData = readTagExp(xmlData, i, false, "?>"); | |
| if (!tagData) throw new Error("Pi Tag is not closed."); |
tagExpWithClosingIndex jumps from any quote to the next matching one before it looks for the closing sequence:
fast-xml-parser/src/xmlparser/OrderedObjParser.js
Lines 727 to 730 in d92f44f
| if (code === 34 || code === 39) { // " or ' | |
| // Skip the quoted value in one step: nothing inside it can close the tag | |
| index = xmlData.indexOf(xmlData[index], index + 1); | |
| if (index === -1) return; |
Suggestion: do not skip quotes when the closing sequence is ?>, for example if (closeCode1 === -1 && (code === 34 || code === 39)). With that change applied locally both inputs give the expected output and <?xml version="1.0"?> attributes are still parsed. I can open a PR with tests.
Would you like to work on this issue?
- Yes
- No
Activity
github-actions commentedon Nov 14, 2026
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.