Skip to content

Commit 3551927

Browse files
committed
feat: course & section code identifiers, ref #115
also wrap some if statement in hasDirectText to avoid problems with empty elements
1 parent a418fc1 commit 3551927

2 files changed

Lines changed: 48 additions & 5 deletions

File tree

collection-export/syllabus.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,36 @@ export function addGenre(doc) {
3131
return doc
3232
}
3333

34+
/**
35+
* local/courseInfo/courseName -> mods/identifier[@type="course number"]
36+
* local/courseInfo/section -> mods/identifier[@type="section"]
37+
*
38+
* @param {Document} doc XML document
39+
*
40+
* @returns {Document} transformed document
41+
*/
42+
function addIdentifiers(doc) {
43+
const courseCode = safeSelectFirst("//local/courseInfo/courseName", doc)
44+
const sectionCode = safeSelectFirst("//local/courseInfo/section", doc)
45+
const mods = safeSelectFirst("//mods", doc)
46+
47+
if (hasDirectTextContent(courseCode)) {
48+
const identifier = createElement(doc, 'identifier')
49+
identifier.setAttribute('type', 'course number')
50+
identifier.textContent = courseCode.textContent
51+
mods.appendChild(identifier)
52+
}
53+
54+
if (hasDirectTextContent(sectionCode)) {
55+
const identifier = createElement(doc, 'identifier')
56+
identifier.setAttribute('type', 'section')
57+
identifier.textContent = sectionCode.textContent
58+
mods.appendChild(identifier)
59+
}
60+
61+
return doc
62+
}
63+
3464
/**
3565
* semester -> mods/subject/temporal
3666
* local/department -> strip degree postfix -> mods/subject/topic
@@ -43,7 +73,7 @@ export function addSubjects(doc) {
4373
const department = safeSelectFirst("//local/department", doc)
4474
const mods = safeSelectFirst("//mods", doc)
4575

46-
if (semester) {
76+
if (hasDirectTextContent(semester)) {
4777
const temporal = createElement(doc, 'temporal')
4878
temporal.textContent = semester.textContent
4979
let subjectParent = safeSelectFirst("//mods/subject", doc)
@@ -54,7 +84,7 @@ export function addSubjects(doc) {
5484
subjectParent.appendChild(temporal)
5585
}
5686

57-
if (department) {
87+
if (hasDirectTextContent(department)) {
5888
// TODO we could have a better map of department names to subject terms with URIs
5989
// TODO terms like "Individualized" are not informative
6090
const topic = createElement(doc, 'topic')
@@ -160,11 +190,9 @@ export function convertSyllabusXMLtoMODS(xmlString) {
160190
// mods/part/number to part/text @type=attachment-uuid
161191
convertPartNumbers(doc)
162192

163-
// Add syllabi genre
164193
addGenre(doc)
165-
166-
// Add subjects
167194
addSubjects(doc)
195+
addIdentifiers(doc)
168196

169197
// Remove empty elements last, after all transformations are complete
170198
removeEmptyElements(doc)

collection-export/syllabus.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ import {convertSyllabusXMLtoMODS} from './syllabus.js'
66
// helper function to wrap XML in a root </xml> element
77
const x = (xml) => `<xml>${xml}</xml>`
88

9+
describe('addIdentifiers', () => {
10+
it('should add course number and section identifiers', async () => {
11+
const courseNumber = 'ARTED-101'
12+
const section = 'ARTED-101-1'
13+
const inputXML = x(`<local><courseInfo><courseName>${courseNumber}</courseName><section>${section}</section></courseInfo></local>`)
14+
const result = convertSyllabusXMLtoMODS(inputXML)
15+
const courseIdentifier = xpath.select1('//mods/identifier[@type="course number"]', result)
16+
assert.ok(courseIdentifier)
17+
assert.strictEqual(courseIdentifier.textContent, courseNumber)
18+
const sectionIdentifier = xpath.select1('//mods/identifier[@type="section"]', result)
19+
assert.ok(sectionIdentifier)
20+
assert.strictEqual(sectionIdentifier.textContent, section)
21+
})
22+
})
23+
924
describe('addSubjects', () => {
1025
it('should add a temporal subject for semester', async () => {
1126
const semester = 'Fall 2026'

0 commit comments

Comments
 (0)