Skip to content

Commit

Permalink
added support for ignoring fields from xml document
Browse files Browse the repository at this point in the history
  • Loading branch information
dakbhavesh authored and Bhavesh Shah committed Jun 8, 2024
1 parent b68b60f commit 812da70
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 0 deletions.
42 changes: 42 additions & 0 deletions epcis_event_hash_generator/xml_to_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/"}
Expand Down Expand Up @@ -168,13 +169,46 @@ def _xml_to_py(root, sort=True):
return obj


def remove_xml_declaration(xml_string):
"""
Removes the <?xml> tag from the beginning of an XML string if present.
"""
if xml_string.startswith("<?xml"):
# Find the end of the processing instruction
end_pos = xml_string.find("?>") + 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
"""
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")
Expand All @@ -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.")
Expand Down
2 changes: 2 additions & 0 deletions tests/examples/epcisXmlDocHavingEventWithIgnoreFields.hashes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ni:///sha-256;cec19d047cf3b05d02cf719d9c35bc468e911d7b9f5d415ba67a074a104c28d2?ver=CBV2.0
ni:///sha-256;cec19d047cf3b05d02cf719d9c35bc468e911d7b9f5d415ba67a074a104c28d2?ver=CBV2.0
Original file line number Diff line number Diff line change
@@ -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

59 changes: 59 additions & 0 deletions tests/examples/epcisXmlDocHavingEventWithIgnoreFields.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<epcis:EPCISDocument xmlns:epcis="urn:epcglobal:epcis:xsd:2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cbv="https://ref.gs1.org/cbv/"
schemaVersion="2.0"
creationDate="2023-01-11T12:00:00.000+01:00"
xsi:schemaLocation="urn:epcglobal:epcis:xsd:2 EPCglobal-epcis-2_0.xsd"
xmlns:repository-x="https://repository-x.example.com/"
xmlns:example="https://ns.example.com/epcis/">
<repository-x:ignoreFields>
<example:testField1/>
</repository-x:ignoreFields>
<EPCISBody>
<EventList>
<ObjectEvent>
<eventTime>2015-03-30T10:58:56.591+02:00</eventTime>
<recordTime>2015-03-30T15:38:32.052+02:00</recordTime>
<eventTimeZoneOffset>+02:00</eventTimeZoneOffset>
<epcList/>
<action>ADD</action>
<bizStep>urn:epcglobal:cbv:bizstep:commissioning</bizStep>
<readPoint>
<id>urn:epc:id:sgln:4054738.00005.0</id>
</readPoint>
<extension>
<quantityList>
<quantityElement>
<epcClass>urn:epc:class:lgtin:4054739.099961.2015-03-30a</epcClass>
<quantity>150</quantity>
<uom>KGM</uom>
</quantityElement>
</quantityList>
</extension>
<example:testField1>123</example:testField1>
<example:testField2>ABC</example:testField2>
</ObjectEvent>
<ObjectEvent>
<eventTime>2015-03-30T10:58:56.591+02:00</eventTime>
<recordTime>2015-03-30T15:38:32.052+02:00</recordTime>
<eventTimeZoneOffset>+02:00</eventTimeZoneOffset>
<epcList/>
<action>ADD</action>
<bizStep>urn:epcglobal:cbv:bizstep:commissioning</bizStep>
<readPoint>
<id>urn:epc:id:sgln:4054738.00005.0</id>
</readPoint>
<extension>
<quantityList>
<quantityElement>
<epcClass>urn:epc:class:lgtin:4054739.099961.2015-03-30a</epcClass>
<quantity>150</quantity>
<uom>KGM</uom>
</quantityElement>
</quantityList>
</extension>
<example:testField2>ABC</example:testField2>
</ObjectEvent>
</EventList>
</EPCISBody>
</epcis:EPCISDocument>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ni:///sha-256;cec19d047cf3b05d02cf719d9c35bc468e911d7b9f5d415ba67a074a104c28d2?ver=CBV2.0
ni:///sha-256;cec19d047cf3b05d02cf719d9c35bc468e911d7b9f5d415ba67a074a104c28d2?ver=CBV2.0
Original file line number Diff line number Diff line change
@@ -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

60 changes: 60 additions & 0 deletions tests/examples/epcisXmlQueryDocHavingEventWithIgnoreFields.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<epcisq:EPCISQueryDocument xmlns:epcisq="urn:epcglobal:epcis-query:xsd:2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cbvmda="urn:epcglobal:cbv:mda"
xmlns:sbdh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"
schemaVersion="2.0"
creationDate="2005-07-11T11:30:47.0Z"
xmlns:repository-x="https://repository-x.example.com/"
xmlns:example="https://ns.example.com/epcis/">
<repository-x:ignoreFields>
<example:testField1/>
</repository-x:ignoreFields>
<queryName>SimpleEventQuery</queryName>
<resultsBody>
<EventList>
<ObjectEvent>
<eventTime>2015-03-30T10:58:56.591+02:00</eventTime>
<recordTime>2015-03-30T15:38:32.052+02:00</recordTime>
<eventTimeZoneOffset>+02:00</eventTimeZoneOffset>
<epcList/>
<action>ADD</action>
<bizStep>urn:epcglobal:cbv:bizstep:commissioning</bizStep>
<readPoint>
<id>urn:epc:id:sgln:4054738.00005.0</id>
</readPoint>
<extension>
<quantityList>
<quantityElement>
<epcClass>urn:epc:class:lgtin:4054739.099961.2015-03-30a</epcClass>
<quantity>150</quantity>
<uom>KGM</uom>
</quantityElement>
</quantityList>
</extension>
<example:testField1>123</example:testField1>
<example:testField2>ABC</example:testField2>
</ObjectEvent>
<ObjectEvent>
<eventTime>2015-03-30T10:58:56.591+02:00</eventTime>
<recordTime>2015-03-30T15:38:32.052+02:00</recordTime>
<eventTimeZoneOffset>+02:00</eventTimeZoneOffset>
<epcList/>
<action>ADD</action>
<bizStep>urn:epcglobal:cbv:bizstep:commissioning</bizStep>
<readPoint>
<id>urn:epc:id:sgln:4054738.00005.0</id>
</readPoint>
<extension>
<quantityList>
<quantityElement>
<epcClass>urn:epc:class:lgtin:4054739.099961.2015-03-30a</epcClass>
<quantity>150</quantity>
<uom>KGM</uom>
</quantityElement>
</quantityList>
</extension>
<example:testField2>ABC</example:testField2>
</ObjectEvent>
</EventList>
</resultsBody>
</epcisq:EPCISQueryDocument>

0 comments on commit 812da70

Please sign in to comment.