Skip to content

Commit 46d9569

Browse files
authored
Merge pull request #11 from qmuntal/core-props
Add xsi:type attribute to core props dates
2 parents fa5bb71 + 6954cd1 commit 46d9569

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

package.go

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -197,25 +197,26 @@ func (c *contentTypes) findType(name string) (string, error) {
197197
}
198198

199199
type corePropertiesXMLMarshal struct {
200-
XMLName xml.Name `xml:"coreProperties"`
201-
XML string `xml:"xmlns,attr"`
202-
XMLDCTERMS string `xml:"xmlns:dcterms,attr"`
203-
XMLDC string `xml:"xmlns:dc,attr"`
204-
Category string `xml:"category,omitempty"`
205-
ContentStatus string `xml:"contentStatus,omitempty"`
206-
Created string `xml:"dcterms:created,omitempty"`
207-
Creator string `xml:"dc:creator,omitempty"`
208-
Description string `xml:"dc:description,omitempty"`
209-
Identifier string `xml:"dc:identifier,omitempty"`
210-
Keywords string `xml:"keywords,omitempty"`
211-
Language string `xml:"dc:language,omitempty"`
212-
LastModifiedBy string `xml:"lastModifiedBy,omitempty"`
213-
LastPrinted string `xml:"lastPrinted,omitempty"`
214-
Modified string `xml:"dcterms:modified,omitempty"`
215-
Revision string `xml:"revision,omitempty"`
216-
Subject string `xml:"dc:subject,omitempty"`
217-
Title string `xml:"dc:title,omitempty"`
218-
Version string `xml:"version,omitempty"`
200+
XMLName xml.Name `xml:"coreProperties"`
201+
XML string `xml:"xmlns,attr"`
202+
XMLDCTERMS string `xml:"xmlns:dcterms,attr"`
203+
XMLDC string `xml:"xmlns:dc,attr"`
204+
XMLXSI string `xml:"xmlns:xsi,attr"`
205+
Category string `xml:"category,omitempty"`
206+
ContentStatus string `xml:"contentStatus,omitempty"`
207+
Created w3CDateTime `xml:"dcterms:created,omitempty"`
208+
Creator string `xml:"dc:creator,omitempty"`
209+
Description string `xml:"dc:description,omitempty"`
210+
Identifier string `xml:"dc:identifier,omitempty"`
211+
Keywords string `xml:"keywords,omitempty"`
212+
Language string `xml:"dc:language,omitempty"`
213+
LastModifiedBy string `xml:"lastModifiedBy,omitempty"`
214+
LastPrinted w3CDateTime `xml:"lastPrinted,omitempty"`
215+
Modified w3CDateTime `xml:"dcterms:modified,omitempty"`
216+
Revision string `xml:"revision,omitempty"`
217+
Subject string `xml:"dc:subject,omitempty"`
218+
Title string `xml:"dc:title,omitempty"`
219+
Version string `xml:"version,omitempty"`
219220
}
220221

221222
type corePropertiesXMLUnmarshal struct {
@@ -240,6 +241,16 @@ type corePropertiesXMLUnmarshal struct {
240241
Version string `xml:"version,omitempty"`
241242
}
242243

244+
type w3CDateTime string
245+
246+
func (s w3CDateTime) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
247+
type xmlType struct {
248+
XSITYPE string `xml:"xsi:type,attr"`
249+
Value string `xml:",chardata"`
250+
}
251+
return e.EncodeElement(xmlType{"dcterms:W3CDTF", string(s)}, start)
252+
}
253+
243254
// CoreProperties enable users to get and set well-known and common sets of property metadata within packages.
244255
type CoreProperties struct {
245256
PartName string // Won't be writed to the package, only used to indicate the location of the CoreProperties part. If empty the default location is "/props/core.xml".
@@ -269,10 +280,11 @@ func (c *CoreProperties) encode(w io.Writer) error {
269280
"http://schemas.openxmlformats.org/package/2006/metadata/core-properties",
270281
"http://purl.org/dc/terms/",
271282
"http://purl.org/dc/elements/1.1/",
272-
c.Category, c.ContentStatus, c.Created,
283+
"http://www.w3.org/2001/XMLSchema-instance",
284+
c.Category, c.ContentStatus, w3CDateTime(c.Created),
273285
c.Creator, c.Description, c.Identifier,
274286
c.Keywords, c.Language, c.LastModifiedBy,
275-
c.LastPrinted, c.Modified, c.Revision,
287+
w3CDateTime(c.LastPrinted), w3CDateTime(c.Modified), c.Revision,
276288
c.Subject, c.Title, c.Version,
277289
})
278290
}

package_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ func TestPackage_add(t *testing.T) {
9292
func buildCoreString(content string) string {
9393
s := `<?xml version="1.0" encoding="UTF-8"?>` + "\n"
9494
s += `<coreProperties xmlns="http://schemas.openxmlformats.org/package/2006/metadata/core-properties"`
95-
s += ` xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dc="http://purl.org/dc/elements/1.1/">`
95+
s += ` xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dc="http://purl.org/dc/elements/1.1/"`
96+
s += ` xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">`
9697
return s + content + "</coreProperties>"
9798
}
9899

@@ -106,21 +107,21 @@ func TestCoreProperties_encode(t *testing.T) {
106107
{"empty", &CoreProperties{}, buildCoreString(""), false},
107108
{"some", &CoreProperties{Category: "A", LastPrinted: "b"}, buildCoreString(`
108109
<category>A</category>
109-
<lastPrinted>b</lastPrinted>
110+
<lastPrinted xsi:type="dcterms:W3CDTF">b</lastPrinted>
110111
`), false},
111112
{"all", &CoreProperties{"partName", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o"},
112113
buildCoreString(`
113114
<category>a</category>
114115
<contentStatus>b</contentStatus>
115-
<dcterms:created>c</dcterms:created>
116+
<dcterms:created xsi:type="dcterms:W3CDTF">c</dcterms:created>
116117
<dc:creator>d</dc:creator>
117118
<dc:description>e</dc:description>
118119
<dc:identifier>f</dc:identifier>
119120
<keywords>g</keywords>
120121
<dc:language>h</dc:language>
121122
<lastModifiedBy>i</lastModifiedBy>
122-
<lastPrinted>j</lastPrinted>
123-
<dcterms:modified>k</dcterms:modified>
123+
<lastPrinted xsi:type="dcterms:W3CDTF">j</lastPrinted>
124+
<dcterms:modified xsi:type="dcterms:W3CDTF">k</dcterms:modified>
124125
<revision>l</revision>
125126
<dc:subject>m</dc:subject>
126127
<dc:title>n</dc:title>

0 commit comments

Comments
 (0)