Getting "expected element <workbook> in name space http://schemas.openxmlformats.org/spreadsheetml/2006/main but have no name space" FileOpen and SaveAs afterwards #1886
Labels
enhancement
New feature or request
Description
Hi! We have a problem with reading a file with Excelize, after making a copy of it with Excelize — we are getting
expected element <workbook> in name space http://schemas.openxmlformats.org/spreadsheetml/2006/main but have no name space
error. Detailed description below.Steps to reproduce the issue:
Step zero:
We have a file generated by .NET solution https://github.com/ClosedXML/ClosedXML. Here is an example file (in our exact case it is much bigger, but that's doesn't matter here) - original2.xlsx. This file can be opened fine with Numbers.app, version 11.1 (7031.0.102) on macOS 14.2.1 (23C71).
Step one:
We have a small debug app to pinpoint the bug:
If we run it we'll get an error:
Numbers.app also failing to open it:

Describe the results you received:
expected element <workbook> in name space http://schemas.openxmlformats.org/spreadsheetml/2006/main but have no name space
Describe the results you expected:
File opened without errors.
Output of
go version
:Excelize version or commit ID:
Investingation:
Interesting thing, that this
original2.xlsx
hasxl/workbook.xml
with pretty interestingxmlns
(providing only the very beginning of it, also formatted for the sake of readability):So we have requires NS
http://schemas.openxmlformats.org/spreadsheetml/2006/main
but it is prefixed withx
, so do all elements in file.And after the saving this XML becomes this one (again, providing only the very beginning of it, also formatted for the sake of readability):
Do you see have we still have or
xmlns:x
but all our elements have no such prefix? And there is no any sign ofxmlns
without prefix. That was my first though and I've decided to look at the part were Excelize saving thexl/workbook.xml
.And I found this part:
excelize/workbook.go
Lines 227 to 229 in 055349d
which lead me to
excelize/lib.go
Lines 685 to 692 in 055349d
replaceNameSpaceBytes
in this case replaced usualxmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
(which came from the XMLName field of the workbook structure) with original to namespaces, BUT we are also using thex
prefixes in all elements. And that, as far as I understand, causes the errors.I tried to remove part with finding the
targetXmlns
and after that file started to open perfectly fine. Yes
xl/workbook.xml
will look awful, as it will have all possible XMLNs from the constant, but it works… But I still not sure if this is a proper fix.Possible fix:
We probably need to always add
xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
, unless it is already there. But that's just an idea, maybe you have a better solution for that.The text was updated successfully, but these errors were encountered: