diff --git a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/einvoice/KSeFAdapter.java b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/einvoice/KSeFAdapter.java index cccf00b..934ba2b 100644 --- a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/einvoice/KSeFAdapter.java +++ b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/einvoice/KSeFAdapter.java @@ -246,6 +246,12 @@ public class KSeFAdapter implements SignalAdapter { // Due date - written at the end of the XML tree model.setDueDateTime(workitem.getItemValueLocalDate("invoice.duedate")); + // Strip all XML comments from the document. The template comments + // are useful for template maintainers but should not appear in + // the final invoice that is submitted to KSeF or reviewed by + // tax advisors. + stripComments(model.getRoot().getOwnerDocument()); + // Persist the modified template fileDataXMLTemplate.setContent(model.getContent()); @@ -537,4 +543,38 @@ public class KSeFAdapter implements SignalAdapter { return null; } + + /** + * Removes all XML comment nodes from the given document. + *
+ * The KSeF template carries comments to help template maintainers
+ * (field explanations, fixed-value markers, business rules). These
+ * comments must not appear in the final invoice that is submitted
+ * to KSeF or reviewed by the tax advisor - they only add noise.
+ */
+ private void stripComments(Document doc) {
+ if (doc == null) {
+ return;
+ }
+ removeCommentsRecursive(doc);
+ }
+
+ /**
+ * Recursively walks a node and removes every direct child that is an
+ * XML comment. Iterates from the last child backwards so that
+ * removing a node does not affect the index of the still-to-visit
+ * children.
+ */
+ private void removeCommentsRecursive(Node node) {
+ Node child = node.getLastChild();
+ while (child != null) {
+ Node previous = child.getPreviousSibling();
+ if (child.getNodeType() == Node.COMMENT_NODE) {
+ node.removeChild(child);
+ } else if (child.hasChildNodes()) {
+ removeCommentsRecursive(child);
+ }
+ child = previous;
+ }
+ }
}
\ No newline at end of file
diff --git a/office-alexander-logistics-app/src/test/resources/ksef/ksef.xml b/office-alexander-logistics-app/src/test/resources/ksef/ksef.xml
index 5901954..6970861 100644
--- a/office-alexander-logistics-app/src/test/resources/ksef/ksef.xml
+++ b/office-alexander-logistics-app/src/test/resources/ksef/ksef.xml
@@ -40,7 +40,7 @@