diff --git a/web-common/src/main/java/edu/unc/lib/boxc/web/common/services/XmlDocumentFilteringService.java b/web-common/src/main/java/edu/unc/lib/boxc/web/common/services/XmlDocumentFilteringService.java index 6f086f97e3..7b1b10d0e0 100644 --- a/web-common/src/main/java/edu/unc/lib/boxc/web/common/services/XmlDocumentFilteringService.java +++ b/web-common/src/main/java/edu/unc/lib/boxc/web/common/services/XmlDocumentFilteringService.java @@ -51,7 +51,8 @@ public void filterExclusions(Document doc) { Element detachEl = el; Element parentEl = detachEl.getParentElement(); // The parent is empty if it has no children other than the element being removed and no attributes - while (parentEl != null && parentEl.getChildren().size() == 1 && parentEl.getAttributes().isEmpty()) { + while (parentEl != null && !parentEl.isRootElement() && parentEl.getChildren().size() == 1 + && parentEl.getAttributes().isEmpty()) { detachEl = parentEl; parentEl = detachEl.getParentElement(); } diff --git a/web-common/src/test/java/edu/unc/lib/boxc/web/common/services/XmlDocumentFilteringServiceTest.java b/web-common/src/test/java/edu/unc/lib/boxc/web/common/services/XmlDocumentFilteringServiceTest.java index db9534bd11..2daf68e9c1 100644 --- a/web-common/src/test/java/edu/unc/lib/boxc/web/common/services/XmlDocumentFilteringServiceTest.java +++ b/web-common/src/test/java/edu/unc/lib/boxc/web/common/services/XmlDocumentFilteringServiceTest.java @@ -26,6 +26,7 @@ public class XmlDocumentFilteringServiceTest { private XmlDocumentFilteringService service; private Document doc; + private Document exclusionDoc; @TempDir public Path tmpFolder; @@ -35,6 +36,7 @@ public void setup() throws Exception { service = new XmlDocumentFilteringService(); SAXBuilder saxBuilder = SecureXMLFactory.createSAXBuilder(); doc = saxBuilder.build(new File("src/test/resources/mods/test_record.xml")); + exclusionDoc = saxBuilder.build(new File("src/test/resources/mods/exclusion_record.xml")); tmpFolder.resolve("testFolder"); Files.createDirectory(tmpFolder.resolve("testFolder")); } @@ -56,6 +58,23 @@ public void excludeNoMatchesTest() throws Exception { assertEquals(before, after); } + @Test + public void excludeAllElementsTest() throws Exception { + initWithXPath("//mods:originInfo[@displayLabel=\"Digital Scan Date Raw Scan\"]/mods:dateCaptured", + "//mods:originInfo[@displayLabel=\"Digital Scan Date filename\"]/mods:dateCaptured", + "//mods:identifier[@type=\"filename\"]", + "//mods:physicalDescription/mods:note[@type=\"technical\"]", + "//mods:originInfo/mods:place/mods:placeTerm[@type=\"code\" and @authority=\"marccountry\"]", + "//mods:originInfo/mods:dateIssued[@encoding=\"marc\"]", + "//mods:identifier[@displayLabel=\"HookID\" and @type=\"local\"]", + "//mods:physicalDescription/mods:note[@displayLabel=\"Container type\"]", + "//mods:identifier[@displayLabel=\"CONTENTdm number\" and @type=\"local\"]", + "//mods:accessCondition[@type=\"use and reproduction\" and @displayLabel=\"CONTENTdm Usage Rights\"]"); + + service.filterExclusions(exclusionDoc); + assertEquals(0, exclusionDoc.getRootElement().getChildren().size()); + } + @Test public void excludeSingleElementTest() throws Exception { initWithXPath("mods:mods/mods:titleInfo/mods:title"); diff --git a/web-common/src/test/resources/mods/exclusion_record.xml b/web-common/src/test/resources/mods/exclusion_record.xml new file mode 100644 index 0000000000..cd76a26acf --- /dev/null +++ b/web-common/src/test/resources/mods/exclusion_record.xml @@ -0,0 +1,7 @@ + + + 7063/original_file + 7054.jp2 + Volume37/Dwane Powell_026.tif + Dwane Powell_026.tif + \ No newline at end of file