summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxuri <xuri.me@gmail.com>2018-10-17 00:28:31 +0800
committerxuri <xuri.me@gmail.com>2018-10-17 00:28:31 +0800
commit1c45425f12f38012b975c36f4d17bd1cec3c0aba (patch)
tree51b99939364569dd6e1f2341c2395ec5e7af52ed
parentd8a34af384d2fa2493e80bf0259454043b3ff54e (diff)
resolve #276, add OfficeOpenXML-XMLSchema-Strict mode support
-rw-r--r--chart.go2
-rw-r--r--excelize.go2
-rw-r--r--lib.go16
-rw-r--r--picture.go12
-rw-r--r--sheet.go8
-rw-r--r--styles.go4
-rwxr-xr-xtest/Book1.xlsxbin23099 -> 33070 bytes
-rw-r--r--xmlDrawing.go41
8 files changed, 53 insertions, 32 deletions
diff --git a/chart.go b/chart.go
index 5353a32..edfcab7 100644
--- a/chart.go
+++ b/chart.go
@@ -1097,7 +1097,7 @@ func (f *File) drawingParser(drawingXML string, content *xlsxWsDr) int {
_, ok := f.XLSX[drawingXML]
if ok { // Append Model
decodeWsDr := decodeWsDr{}
- _ = xml.Unmarshal([]byte(f.readXML(drawingXML)), &decodeWsDr)
+ _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(drawingXML)), &decodeWsDr)
content.R = decodeWsDr.R
cNvPrID = len(decodeWsDr.OneCellAnchor) + len(decodeWsDr.TwoCellAnchor) + 1
for _, v := range decodeWsDr.OneCellAnchor {
diff --git a/excelize.go b/excelize.go
index d1f0b7f..36a6d8a 100644
--- a/excelize.go
+++ b/excelize.go
@@ -100,7 +100,7 @@ func (f *File) workSheetReader(sheet string) *xlsxWorksheet {
}
if f.Sheet[name] == nil {
var xlsx xlsxWorksheet
- _ = xml.Unmarshal(f.readXML(name), &xlsx)
+ _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(name)), &xlsx)
if f.checked == nil {
f.checked = make(map[string]bool)
}
diff --git a/lib.go b/lib.go
index 865ee29..8e63da9 100644
--- a/lib.go
+++ b/lib.go
@@ -183,3 +183,19 @@ func parseFormatSet(formatSet string) []byte {
}
return []byte("{}")
}
+
+// namespaceStrictToTransitional provides a method to convert Strict and
+// Transitional namespaces.
+func namespaceStrictToTransitional(content []byte) []byte {
+ var namespaceTranslationDic = map[string]string{
+ StrictSourceRelationship: SourceRelationship,
+ StrictSourceRelationshipChart: SourceRelationshipChart,
+ StrictSourceRelationshipComments: SourceRelationshipComments,
+ StrictSourceRelationshipImage: SourceRelationshipImage,
+ StrictNameSpaceSpreadSheet: NameSpaceSpreadSheet,
+ }
+ for s, n := range namespaceTranslationDic {
+ content = bytes.Replace(content, []byte(s), []byte(n), -1)
+ }
+ return content
+}
diff --git a/picture.go b/picture.go
index 8785aaf..9efd875 100644
--- a/picture.go
+++ b/picture.go
@@ -185,7 +185,7 @@ func (f *File) addSheetRelationships(sheet, relType, target, targetMode string)
_, ok = f.XLSX[rels]
if ok {
ID.Reset()
- _ = xml.Unmarshal([]byte(f.readXML(rels)), &sheetRels)
+ _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(rels)), &sheetRels)
rID = len(sheetRels.Relationships) + 1
ID.WriteString("rId")
ID.WriteString(strconv.Itoa(rID))
@@ -211,7 +211,7 @@ func (f *File) deleteSheetRelationships(sheet, rID string) {
}
var rels = "xl/worksheets/_rels/" + strings.TrimPrefix(name, "xl/worksheets/") + ".rels"
var sheetRels xlsxWorkbookRels
- _ = xml.Unmarshal([]byte(f.readXML(rels)), &sheetRels)
+ _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(rels)), &sheetRels)
for k, v := range sheetRels.Relationships {
if v.ID == rID {
sheetRels.Relationships = append(sheetRels.Relationships[:k], sheetRels.Relationships[k+1:]...)
@@ -328,7 +328,7 @@ func (f *File) addDrawingRelationships(index int, relType, target, targetMode st
_, ok := f.XLSX[rels]
if ok {
ID.Reset()
- _ = xml.Unmarshal([]byte(f.readXML(rels)), &drawingRels)
+ _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(rels)), &drawingRels)
rID = len(drawingRels.Relationships) + 1
ID.WriteString("rId")
ID.WriteString(strconv.Itoa(rID))
@@ -448,7 +448,7 @@ func (f *File) getSheetRelationshipsTargetByID(sheet, rID string) string {
}
var rels = "xl/worksheets/_rels/" + strings.TrimPrefix(name, "xl/worksheets/") + ".rels"
var sheetRels xlsxWorkbookRels
- _ = xml.Unmarshal([]byte(f.readXML(rels)), &sheetRels)
+ _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(rels)), &sheetRels)
for _, v := range sheetRels.Relationships {
if v.ID == rID {
return v.Target
@@ -488,7 +488,7 @@ func (f *File) GetPicture(sheet, cell string) (string, []byte) {
return "", nil
}
decodeWsDr := decodeWsDr{}
- _ = xml.Unmarshal([]byte(f.readXML(drawingXML)), &decodeWsDr)
+ _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(drawingXML)), &decodeWsDr)
cell = strings.ToUpper(cell)
fromCol := string(strings.Map(letterOnlyMapF, cell))
@@ -523,7 +523,7 @@ func (f *File) getDrawingRelationships(rels, rID string) *xlsxWorkbookRelation {
return nil
}
var drawingRels xlsxWorkbookRels
- _ = xml.Unmarshal([]byte(f.readXML(rels)), &drawingRels)
+ _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(rels)), &drawingRels)
for _, v := range drawingRels.Relationships {
if v.ID == rID {
return &v
diff --git a/sheet.go b/sheet.go
index 2344218..7b97d3e 100644
--- a/sheet.go
+++ b/sheet.go
@@ -51,7 +51,7 @@ func (f *File) NewSheet(name string) int {
func (f *File) contentTypesReader() *xlsxTypes {
if f.ContentTypes == nil {
var content xlsxTypes
- _ = xml.Unmarshal([]byte(f.readXML("[Content_Types].xml")), &content)
+ _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML("[Content_Types].xml")), &content)
f.ContentTypes = &content
}
return f.ContentTypes
@@ -71,7 +71,7 @@ func (f *File) contentTypesWriter() {
func (f *File) workbookReader() *xlsxWorkbook {
if f.WorkBook == nil {
var content xlsxWorkbook
- _ = xml.Unmarshal([]byte(f.readXML("xl/workbook.xml")), &content)
+ _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML("xl/workbook.xml")), &content)
f.WorkBook = &content
}
return f.WorkBook
@@ -162,7 +162,7 @@ func (f *File) setWorkbook(name string, rid int) {
func (f *File) workbookRelsReader() *xlsxWorkbookRels {
if f.WorkBookRels == nil {
var content xlsxWorkbookRels
- _ = xml.Unmarshal([]byte(f.readXML("xl/_rels/workbook.xml.rels")), &content)
+ _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML("xl/_rels/workbook.xml.rels")), &content)
f.WorkBookRels = &content
}
return f.WorkBookRels
@@ -267,7 +267,7 @@ func (f *File) GetActiveSheetIndex() int {
buffer.WriteString("xl/worksheets/sheet")
buffer.WriteString(strings.TrimPrefix(v.ID, "rId"))
buffer.WriteString(".xml")
- _ = xml.Unmarshal([]byte(f.readXML(buffer.String())), &xlsx)
+ _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(buffer.String())), &xlsx)
for _, sheetView := range xlsx.SheetViews.SheetView {
if sheetView.TabSelected {
ID, _ := strconv.Atoi(strings.TrimPrefix(v.ID, "rId"))
diff --git a/styles.go b/styles.go
index 513fc9b..f923787 100644
--- a/styles.go
+++ b/styles.go
@@ -999,7 +999,7 @@ func is12HourTime(format string) bool {
func (f *File) stylesReader() *xlsxStyleSheet {
if f.Styles == nil {
var styleSheet xlsxStyleSheet
- _ = xml.Unmarshal([]byte(f.readXML("xl/styles.xml")), &styleSheet)
+ _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML("xl/styles.xml")), &styleSheet)
f.Styles = &styleSheet
}
return f.Styles
@@ -2757,7 +2757,7 @@ func getPaletteColor(color string) string {
// structure after deserialization.
func (f *File) themeReader() *xlsxTheme {
var theme xlsxTheme
- _ = xml.Unmarshal([]byte(f.readXML("xl/theme/theme1.xml")), &theme)
+ _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML("xl/theme/theme1.xml")), &theme)
return &theme
}
diff --git a/test/Book1.xlsx b/test/Book1.xlsx
index 84c43d1..7d9886e 100755
--- a/test/Book1.xlsx
+++ b/test/Book1.xlsx
Binary files differ
diff --git a/xmlDrawing.go b/xmlDrawing.go
index 6ba7d31..7356cb5 100644
--- a/xmlDrawing.go
+++ b/xmlDrawing.go
@@ -13,24 +13,29 @@ import "encoding/xml"
// Source relationship and namespace.
const (
- SourceRelationship = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
- SourceRelationshipChart = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart"
- SourceRelationshipComments = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments"
- SourceRelationshipImage = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
- SourceRelationshipTable = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/table"
- SourceRelationshipDrawingML = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing"
- SourceRelationshipDrawingVML = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing"
- SourceRelationshipHyperLink = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
- SourceRelationshipWorkSheet = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"
- SourceRelationshipChart201506 = "http://schemas.microsoft.com/office/drawing/2015/06/chart"
- SourceRelationshipChart20070802 = "http://schemas.microsoft.com/office/drawing/2007/8/2/chart"
- SourceRelationshipChart2014 = "http://schemas.microsoft.com/office/drawing/2014/chart"
- SourceRelationshipCompatibility = "http://schemas.openxmlformats.org/markup-compatibility/2006"
- NameSpaceDrawingML = "http://schemas.openxmlformats.org/drawingml/2006/main"
- NameSpaceDrawingMLChart = "http://schemas.openxmlformats.org/drawingml/2006/chart"
- NameSpaceDrawingMLSpreadSheet = "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"
- NameSpaceSpreadSheet = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"
- NameSpaceXML = "http://www.w3.org/XML/1998/namespace"
+ SourceRelationship = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
+ SourceRelationshipChart = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart"
+ SourceRelationshipComments = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments"
+ SourceRelationshipImage = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
+ SourceRelationshipTable = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/table"
+ SourceRelationshipDrawingML = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing"
+ SourceRelationshipDrawingVML = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing"
+ SourceRelationshipHyperLink = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
+ SourceRelationshipWorkSheet = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"
+ SourceRelationshipChart201506 = "http://schemas.microsoft.com/office/drawing/2015/06/chart"
+ SourceRelationshipChart20070802 = "http://schemas.microsoft.com/office/drawing/2007/8/2/chart"
+ SourceRelationshipChart2014 = "http://schemas.microsoft.com/office/drawing/2014/chart"
+ SourceRelationshipCompatibility = "http://schemas.openxmlformats.org/markup-compatibility/2006"
+ NameSpaceDrawingML = "http://schemas.openxmlformats.org/drawingml/2006/main"
+ NameSpaceDrawingMLChart = "http://schemas.openxmlformats.org/drawingml/2006/chart"
+ NameSpaceDrawingMLSpreadSheet = "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"
+ NameSpaceSpreadSheet = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"
+ NameSpaceXML = "http://www.w3.org/XML/1998/namespace"
+ StrictSourceRelationship = "http://purl.oclc.org/ooxml/officeDocument/relationships"
+ StrictSourceRelationshipChart = "http://purl.oclc.org/ooxml/officeDocument/relationships/chart"
+ StrictSourceRelationshipComments = "http://purl.oclc.org/ooxml/officeDocument/relationships/comments"
+ StrictSourceRelationshipImage = "http://purl.oclc.org/ooxml/officeDocument/relationships/image"
+ StrictNameSpaceSpreadSheet = "http://purl.oclc.org/ooxml/spreadsheetml/main"
)
var supportImageTypes = map[string]string{".gif": ".gif", ".jpg": ".jpeg", ".jpeg": ".jpeg", ".png": ".png"}