From 99166ee398170d321bd557ae25f699e33d97361a Mon Sep 17 00:00:00 2001 From: May Ben-Arie Date: Mon, 15 Jul 2024 10:20:23 +0300 Subject: [PATCH] fix: namespaced xmls --- .../inputConverters/xml/xmlToJson.test.ts | 23 ++++++++++++++----- src/helpers/inputConverters/xml/xmlToJson.ts | 11 ++++++--- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/helpers/inputConverters/xml/xmlToJson.test.ts b/src/helpers/inputConverters/xml/xmlToJson.test.ts index ede331a..1a65979 100644 --- a/src/helpers/inputConverters/xml/xmlToJson.test.ts +++ b/src/helpers/inputConverters/xml/xmlToJson.test.ts @@ -75,14 +75,12 @@ describe('xmlToJson', () => { ); }); - test.skip('namespaced xml', async () => { + test('namespaced xml', async () => { const xml: string = ` kdkdkdkdkdkdkdk TBD - N1AD14A36 - TBD 09909 CREATE 2024-07-10T11:31:34.959+03:00 @@ -115,7 +113,7 @@ describe('xmlToJson', () => { `; expect(parseXml(xml)).toEqual({ - header: { + Header: { TransactionID: 'kdkdkdkdkdkdkdk', _TransactionID: { _namespace: 'tst2' @@ -123,7 +121,16 @@ describe('xmlToJson', () => { TransactionName: 'TBD', _TransactionName: { _namespace: 'tst2' }, HospitalCode: '09909', - _HospitalCode: { _namespace: 'tst2' } + _HospitalCode: { _namespace: 'tst2' }, + EventType: 'CREATE', + _EventType: { + _namespace: 'tst2' + }, + EventDateTime: '2024-07-10T11:31:34.959+03:00', + _EventDateTime: { + _namespace: 'tst2' + }, + _namespace: 'tst2' }, CreatePatientType: { PatientID: '0000000026', @@ -160,6 +167,10 @@ describe('xmlToJson', () => { _namespace: 'tst1' }, _namespace: 'tst1', + 'xmlns:tst1': 'http://test.tst.example.com/HIJ/FIRE/test1.xsd', + 'xmlns:tst2': 'http://test.tst.example.com/HIJ/FIRE/test2.xsd', + 'xmlns:xsd': 'http://www.w3.org/2001/XMLSchema', + 'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance', _xmlTagName: 'Patient' }); }); @@ -242,7 +253,7 @@ describe('xmlToJson', () => { ); }); - test.skip('xml declaration tag', () => { + test('xml declaration tag', () => { const xml: string = ` diff --git a/src/helpers/inputConverters/xml/xmlToJson.ts b/src/helpers/inputConverters/xml/xmlToJson.ts index a69e7fe..58746d0 100644 --- a/src/helpers/inputConverters/xml/xmlToJson.ts +++ b/src/helpers/inputConverters/xml/xmlToJson.ts @@ -34,6 +34,7 @@ export const parseXml = (xml: string) => { const parseXmlWithXhtmlHandling = (xml: string): any => { const options: Record = { ignoreAttributes: false, + ignoreDeclaration: true, attributeNamePrefix: ATTRIBUTE_PREFIX, allowBooleanAttributes: true, alwaysCreateTextNode: true, @@ -76,16 +77,20 @@ const getXhtmlPaths = (currentPath, currentValue, xhtmlPaths) => { const standardizeJson = (json, rootKey: string): Record => { const parsedXml = recursiveStandardize(json, rootKey); - const value = parsedXml[rootKey]; + + const namespaceIndex = rootKey.indexOf(':'); + const baseRootKey = namespaceIndex === -1 ? rootKey : rootKey.slice(namespaceIndex + 1); + + const value = parsedXml[baseRootKey]; if (typeof value === 'object') { const namespaceIndex = rootKey.indexOf(':'); if (namespaceIndex !== -1) { value._namespace = rootKey.slice(0, namespaceIndex); if (!value.resourceType) { - value._xmlTagName = rootKey.slice(namespaceIndex + 1); + value._xmlTagName = baseRootKey; } } else if (!value.resourceType) { - value._xmlTagName = rootKey; + value._xmlTagName = baseRootKey; } } return value;