diff --git a/epcis_event_hash_generator/xml_to_py.py b/epcis_event_hash_generator/xml_to_py.py
index 3a9bc0c..d965f9d 100644
--- a/epcis_event_hash_generator/xml_to_py.py
+++ b/epcis_event_hash_generator/xml_to_py.py
@@ -65,6 +65,7 @@
import logging
import xml.etree.ElementTree as ElementTree
+from lxml import etree
from typing import Tuple
_expansions = {"gs1:": "https://gs1.org/voc/", "cbv:": "https://ref.gs1.org/cbv/"}
@@ -168,6 +169,34 @@ def _xml_to_py(root, sort=True):
return obj
+def remove_xml_declaration(xml_string):
+ """
+ Removes the tag from the beginning of an XML string if present.
+ """
+ if xml_string.startswith("") + 2 # Include the ?> characters
+ return xml_string[end_pos:]
+ else:
+ return xml_string
+
+
+def get_ignore_field_prefix_ns(xmlStr: str):
+ """
+ if presents, gets all fields to be ignored from events of EPCIS document.
+ """
+ xml_str_without_decl = remove_xml_declaration(xmlStr)
+
+ all_namespaces = etree.fromstring(xml_str_without_decl).nsmap
+
+ ignore_field_ns_prefix = None
+ for key, value in all_namespaces.items():
+ if value == 'https://repository-x.example.com/':
+ return key
+
+ return ignore_field_ns_prefix
+
+
def event_list_from_epcis_document_str(xmlStr: str) -> Tuple[str, str, list]:
"""
Read EPCIS XML document and generate the event List in the form of a simple python object
@@ -175,6 +204,11 @@ def event_list_from_epcis_document_str(xmlStr: str) -> Tuple[str, str, list]:
try:
data = _remove_extension_tags(xmlStr)
+ ignore_field_ns_prefix = get_ignore_field_prefix_ns(xmlStr)
+
+ if ignore_field_ns_prefix is not None:
+ data = data.replace(ignore_field_ns_prefix + ':', '')
+
root = ElementTree.fromstring(data)
eventList = root.find("*EventList")
@@ -184,6 +218,14 @@ def event_list_from_epcis_document_str(xmlStr: str) -> Tuple[str, str, list]:
if not eventList:
raise ValueError("No EventList found")
+
+ # remove all fields to be ignored
+ for field_to_remove in root.findall("ignoreFields/*"):
+ for event in eventList:
+ el = event.find(field_to_remove.tag)
+ if el is not None:
+ event.remove(el)
+
except (ValueError, OSError) as ex:
logging.error(ex)
logging.error("Input string does not contain a valid EPCIS XML document with EventList.")
diff --git a/tests/examples/epcisXmlDocHavingEventWithIgnoreFields.hashes b/tests/examples/epcisXmlDocHavingEventWithIgnoreFields.hashes
new file mode 100644
index 0000000..6be5ac2
--- /dev/null
+++ b/tests/examples/epcisXmlDocHavingEventWithIgnoreFields.hashes
@@ -0,0 +1,2 @@
+ni:///sha-256;cec19d047cf3b05d02cf719d9c35bc468e911d7b9f5d415ba67a074a104c28d2?ver=CBV2.0
+ni:///sha-256;cec19d047cf3b05d02cf719d9c35bc468e911d7b9f5d415ba67a074a104c28d2?ver=CBV2.0
diff --git a/tests/examples/epcisXmlDocHavingEventWithIgnoreFields.prehashes b/tests/examples/epcisXmlDocHavingEventWithIgnoreFields.prehashes
new file mode 100644
index 0000000..7ec1521
--- /dev/null
+++ b/tests/examples/epcisXmlDocHavingEventWithIgnoreFields.prehashes
@@ -0,0 +1,3 @@
+eventType=ObjectEventeventTime=2015-03-30T08:58:56.591ZeventTimeZoneOffset=+02:00quantityListquantityElementepcClass=https://id.gs1.org/01/04054739999612/10/2015-03-30aquantity=150uom=KGMaction=ADDbizStep=https://ref.gs1.org/cbv/BizStep-commissioningreadPointid=https://id.gs1.org/414/4054738000050{https://ns.example.com/epcis/}testField2=ABC
+eventType=ObjectEventeventTime=2015-03-30T08:58:56.591ZeventTimeZoneOffset=+02:00quantityListquantityElementepcClass=https://id.gs1.org/01/04054739999612/10/2015-03-30aquantity=150uom=KGMaction=ADDbizStep=https://ref.gs1.org/cbv/BizStep-commissioningreadPointid=https://id.gs1.org/414/4054738000050{https://ns.example.com/epcis/}testField2=ABC
+
diff --git a/tests/examples/epcisXmlDocHavingEventWithIgnoreFields.xml b/tests/examples/epcisXmlDocHavingEventWithIgnoreFields.xml
new file mode 100644
index 0000000..b4417b2
--- /dev/null
+++ b/tests/examples/epcisXmlDocHavingEventWithIgnoreFields.xml
@@ -0,0 +1,59 @@
+
+
+
+
+
+
+
+
+ 2015-03-30T10:58:56.591+02:00
+ 2015-03-30T15:38:32.052+02:00
+ +02:00
+
+ ADD
+ urn:epcglobal:cbv:bizstep:commissioning
+
+ urn:epc:id:sgln:4054738.00005.0
+
+
+
+
+ urn:epc:class:lgtin:4054739.099961.2015-03-30a
+ 150
+ KGM
+
+
+
+ 123
+ ABC
+
+
+ 2015-03-30T10:58:56.591+02:00
+ 2015-03-30T15:38:32.052+02:00
+ +02:00
+
+ ADD
+ urn:epcglobal:cbv:bizstep:commissioning
+
+ urn:epc:id:sgln:4054738.00005.0
+
+
+
+
+ urn:epc:class:lgtin:4054739.099961.2015-03-30a
+ 150
+ KGM
+
+
+
+ ABC
+
+
+
+
diff --git a/tests/examples/epcisXmlQueryDocHavingEventWithIgnoreFields.hashes b/tests/examples/epcisXmlQueryDocHavingEventWithIgnoreFields.hashes
new file mode 100644
index 0000000..6be5ac2
--- /dev/null
+++ b/tests/examples/epcisXmlQueryDocHavingEventWithIgnoreFields.hashes
@@ -0,0 +1,2 @@
+ni:///sha-256;cec19d047cf3b05d02cf719d9c35bc468e911d7b9f5d415ba67a074a104c28d2?ver=CBV2.0
+ni:///sha-256;cec19d047cf3b05d02cf719d9c35bc468e911d7b9f5d415ba67a074a104c28d2?ver=CBV2.0
diff --git a/tests/examples/epcisXmlQueryDocHavingEventWithIgnoreFields.prehashes b/tests/examples/epcisXmlQueryDocHavingEventWithIgnoreFields.prehashes
new file mode 100644
index 0000000..7ec1521
--- /dev/null
+++ b/tests/examples/epcisXmlQueryDocHavingEventWithIgnoreFields.prehashes
@@ -0,0 +1,3 @@
+eventType=ObjectEventeventTime=2015-03-30T08:58:56.591ZeventTimeZoneOffset=+02:00quantityListquantityElementepcClass=https://id.gs1.org/01/04054739999612/10/2015-03-30aquantity=150uom=KGMaction=ADDbizStep=https://ref.gs1.org/cbv/BizStep-commissioningreadPointid=https://id.gs1.org/414/4054738000050{https://ns.example.com/epcis/}testField2=ABC
+eventType=ObjectEventeventTime=2015-03-30T08:58:56.591ZeventTimeZoneOffset=+02:00quantityListquantityElementepcClass=https://id.gs1.org/01/04054739999612/10/2015-03-30aquantity=150uom=KGMaction=ADDbizStep=https://ref.gs1.org/cbv/BizStep-commissioningreadPointid=https://id.gs1.org/414/4054738000050{https://ns.example.com/epcis/}testField2=ABC
+
diff --git a/tests/examples/epcisXmlQueryDocHavingEventWithIgnoreFields.xml b/tests/examples/epcisXmlQueryDocHavingEventWithIgnoreFields.xml
new file mode 100644
index 0000000..73f771e
--- /dev/null
+++ b/tests/examples/epcisXmlQueryDocHavingEventWithIgnoreFields.xml
@@ -0,0 +1,60 @@
+
+
+
+
+
+ SimpleEventQuery
+
+
+
+ 2015-03-30T10:58:56.591+02:00
+ 2015-03-30T15:38:32.052+02:00
+ +02:00
+
+ ADD
+ urn:epcglobal:cbv:bizstep:commissioning
+
+ urn:epc:id:sgln:4054738.00005.0
+
+
+
+
+ urn:epc:class:lgtin:4054739.099961.2015-03-30a
+ 150
+ KGM
+
+
+
+ 123
+ ABC
+
+
+ 2015-03-30T10:58:56.591+02:00
+ 2015-03-30T15:38:32.052+02:00
+ +02:00
+
+ ADD
+ urn:epcglobal:cbv:bizstep:commissioning
+
+ urn:epc:id:sgln:4054738.00005.0
+
+
+
+
+ urn:epc:class:lgtin:4054739.099961.2015-03-30a
+ 150
+ KGM
+
+
+
+ ABC
+
+
+
+