parseAttributeValue 将单元格坐标 "E1" 误判为科学计数法并转成 null,导致属性值丢失
Maintainers usually reply within 2 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 65/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- javascript, node.js
- Domain
- data
Research direction
Start with the reproduction: new XMLParser({ parseAttributeValue: true, ignoreAttributes: false }) parsing <hyperlink ref="E1"/> returns null for ref, while F1 and B5 survive. Find where attribute values are converted to numbers and how numberParseOptions.eNotation is applied, since the E1 case is treated as scientific notation. Done means E1 stays the string "E1" by default, other numeric attributes still convert, and a test covers the E1/E10 cases with eNotation on and off.
Written by the indexing model from the issue text.
Description
背景与实际问题
我在开发一个 Android 离线 WebView 里的 .xlsx 只读预览/编辑器,底层用 fast-xml-parser 解析原始 worksheet XML。
xlsx 里的单元格坐标(如 E1)会出现在工作表超链接的属性上:
<hyperlink ref="E1" location="Sheet2!A1"/>
ref="E1"—— 这个超链接贴在哪个单元格(左上角坐标,随所在位置变化,可能是A1/B5/E1/F1…)location="Sheet2!A1"—— 点击后跳转的目标
也就是说,E1 在这里只是一个普通的单元格地址字符串,既不是整数、也不是科学计数法,它本应被当作字符串原样保留。
问题
开启 parseAttributeValue: true 后,fast-xml-parser 会把属性值自动转成数字。它对 E1 的判定是:看起来像科学计数法 E1(= 1×10¹),尝试转换后得到 null,结果把整个坐标属性吃掉了:
const { XMLParser } = require('fast-xml-parser') // 4.5.7 / 4.x
const p = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: '@_',
parseAttributeValue: true,
trimValues: true,
})
p.parse('<hyperlink ref="E1" location="Sheet2!A1"/>')
实际输出:
{ "hyperlink": { "@_ref": null, "@_location": "Sheet2!A1" } }
期望输出(或至少不能丢原值):
{ "hyperlink": { "@_ref": "E1", "@_location": "Sheet2!A1" } }
对比:同一份文件里 ref="F1"、ref="B5" 都能正常保留为字符串,唯独 E1(以及任何形如 字母+E+两位数字 的值)被转成 null。在我这个 xlsx 里,有两张表的返回链接 ref="E1" 因此整条丢失、格子里点不动;而另一张表是 ref="F1" 就正常。
影响面
E1/E2/E10… 这类单元格坐标在 OOXML(表格、图表引用、定义名称等)里极常见。凡是开启 parseAttributeValue 的解析场景,只要出现这类坐标,就会被静默转成 null,造成数据丢失,且很难排查。
临时规避
numberParseOptions: { eNotation: false }
设置后 E1 会被保留为字符串,其它数字属性仍照常转换。
关于 eNotation
这个行为由 numberParseOptions.eNotation(默认 true)控制。默认开启时,很多纯粹是"字符串"的标识值只要长得像科学计数法,就会被当成数字处理——对 XML 这种以字符串属性为主的数据格式,风险较高,且转换失败后直接变 null(而非保留原字符串),进一步放大了问题。
环境
- fast-xml-parser: 4.5.0(本地实测 4.5.7 同样复现)
- Node / 浏览器均复现
(说明:我这边只用到解析能力,不确定在此之外是否有其它依赖;如已存在同类 issue,烦请合并,谢谢。)
- Dominant language
- JavaScript
- Stars
- 3.1k
- Forks
- 398
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 2
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from NaturalIntelligence/fast-xml-parser
-
ignoreDeclaration does not ignore lower case doctype declarationMay be free again A pull request for this issue was closed without being merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
NaturalIntelligence/fast-xml-parser#870 · 2 comments ·
Maintainers usually reply within 2 days
-
Enhance type definition for XMLParser `isArray` option when using TypeScriptMay be free again A pull request for this issue was closed without being merged. Open
Difficulty 3/5 1-2 days Newbie friendliness 52/100
NaturalIntelligence/fast-xml-parser#803 · 21 comments · 2 reactions ·
Maintainers usually reply within 2 days
-
Feature-Request
Difficulty 4/5 3-5 days Newbie friendliness 52/100
NaturalIntelligence/fast-xml-parser#779 · 9 comments ·
Maintainers usually reply within 2 days
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
NaturalIntelligence/fast-xml-parser#766 · 4 comments ·
Maintainers usually reply within 2 days
-
enhancement
Difficulty 3/5 1-2 days Newbie friendliness 48/100
NaturalIntelligence/fast-xml-parser#708 · 4 comments ·
Maintainers usually reply within 2 days
All issues in NaturalIntelligence/fast-xml-parser
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 65/100
-
[BUG] Tools returned by input processors get an empty context.agent.agentIdMay be free again A pull request for this issue was closed without being merged. Open@mastra/core effort:low impact:medium status: auto-triaged Tools
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
mastra-ai/mastra#26794 · 3 comments ·
Maintainers usually reply within 1 day
-
release:v5.34
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
Jason-Vaughan/TangleClaw#2365 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
enhancement
Difficulty 1/5 1-3 hours Newbie friendliness 85/100
holepunchto/hrpc-test#28 ·