resolve #276, add OfficeOpenXML-XMLSchema-Strict mode support

This commit is contained in:
xuri 2018-10-17 00:28:31 +08:00
parent d8a34af384
commit 1c45425f12
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
8 changed files with 53 additions and 32 deletions

View File

@ -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 {

View File

@ -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)
}

16
lib.go
View File

@ -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
}

View File

@ -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

View File

@ -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"))

View File

@ -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
}

Binary file not shown.

View File

@ -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"}