Skip to content

Commit 13e7bce

Browse files
committed
improvement compatibility for the XML ignorable namespace
1 parent ca43c65 commit 13e7bce

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

lib.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,14 @@ func (f *File) replaceNameSpaceBytes(path string, contentMarshal []byte) []byte
379379
func (f *File) addNameSpaces(path string, ns xml.Attr) {
380380
exist := false
381381
mc := false
382-
ignore := false
382+
ignore := -1
383383
if attr, ok := f.xmlAttr[path]; ok {
384-
for _, attribute := range attr {
384+
for i, attribute := range attr {
385385
if attribute.Name.Local == ns.Name.Local && attribute.Name.Space == ns.Name.Space {
386386
exist = true
387387
}
388-
if attribute.Name.Local == "Ignorable" && attribute.Name.Space == "mc" {
389-
ignore = true
388+
if attribute.Name.Local == "Ignorable" && getXMLNamespace(attribute.Name.Space, attr) == "mc" {
389+
ignore = i
390390
}
391391
if attribute.Name.Local == "mc" && attribute.Name.Space == "xmlns" {
392392
mc = true
@@ -398,12 +398,23 @@ func (f *File) addNameSpaces(path string, ns xml.Attr) {
398398
if !mc {
399399
f.xmlAttr[path] = append(f.xmlAttr[path], SourceRelationshipCompatibility)
400400
}
401-
if !ignore {
401+
if ignore == -1 {
402402
f.xmlAttr[path] = append(f.xmlAttr[path], xml.Attr{
403403
Name: xml.Name{Local: "Ignorable", Space: "mc"},
404404
Value: ns.Name.Local,
405405
})
406+
return
406407
}
408+
f.setIgnorableNameSpace(path, ignore, ns)
409+
}
410+
}
411+
412+
// setIgnorableNameSpace provides a function to set XML namespace as ignorable by the given
413+
// attribute.
414+
func (f *File) setIgnorableNameSpace(path string, index int, ns xml.Attr) {
415+
ignorableNS := []string{"c14", "cdr14", "a14", "pic14", "x14", "xdr14", "x14ac", "dsp", "mso14", "dgm14", "x15", "x12ac", "x15ac", "xr", "xr2", "xr3", "xr4", "xr5", "xr6", "xr7", "xr8", "xr9", "xr10", "xr11", "xr12", "xr13", "xr14", "xr15", "x15", "x16", "x16r2", "mo", "mx", "mv", "o", "v"}
416+
if inStrSlice(strings.Fields(f.xmlAttr[path][index].Value), ns.Name.Local) == -1 && inStrSlice(ignorableNS, ns.Name.Local) != -1 {
417+
f.xmlAttr[path][index].Value = strings.TrimSpace(fmt.Sprintf("%s %s", f.xmlAttr[path][index].Value, ns.Name.Local))
407418
}
408419
}
409420

lib_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package excelize
22

33
import (
4+
"encoding/xml"
45
"fmt"
56
"strconv"
67
"strings"
@@ -215,6 +216,13 @@ func TestBytesReplace(t *testing.T) {
215216
assert.EqualValues(t, s, bytesReplace(s, []byte{}, []byte{}, 0))
216217
}
217218

219+
func TestSetIgnorableNameSpace(t *testing.T) {
220+
f := NewFile()
221+
f.xmlAttr["xml_path"] = []xml.Attr{{}}
222+
f.setIgnorableNameSpace("xml_path", 0, xml.Attr{Name: xml.Name{Local: "c14"}})
223+
assert.EqualValues(t, "c14", f.xmlAttr["xml_path"][0].Value)
224+
}
225+
218226
func TestStack(t *testing.T) {
219227
s := NewStack()
220228
assert.Equal(t, s.Peek(), nil)

0 commit comments

Comments
 (0)