Skip to content

Commit

Permalink
fix: namespaced xmls
Browse files Browse the repository at this point in the history
  • Loading branch information
may-ben-arie committed Jul 15, 2024
1 parent f792451 commit 99166ee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
23 changes: 17 additions & 6 deletions src/helpers/inputConverters/xml/xmlToJson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,12 @@ describe('xmlToJson', () => {
);
});

test.skip('namespaced xml', async () => {
test('namespaced xml', async () => {
const xml: string = `
<tst1:Patient 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">
<tst2:Header>
<tst2:TransactionID>kdkdkdkdkdkdkdk</tst2:TransactionID>
<tst2:TransactionName>TBD</tst2:TransactionName>
<tst2:MessageID>N1AD14A36</tst2:MessageID>
<tst2:MessageName>TBD</tst2:MessageName>
<tst2:HospitalCode>09909</tst2:HospitalCode>
<tst2:EventType>CREATE</tst2:EventType>
<tst2:EventDateTime>2024-07-10T11:31:34.959+03:00</tst2:EventDateTime>
Expand Down Expand Up @@ -115,15 +113,24 @@ describe('xmlToJson', () => {
</tst1:Patient>
`;
expect(parseXml(xml)).toEqual({
header: {
Header: {
TransactionID: 'kdkdkdkdkdkdkdk',
_TransactionID: {
_namespace: 'tst2'
},
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',
Expand Down Expand Up @@ -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'
});
});
Expand Down Expand Up @@ -242,7 +253,7 @@ describe('xmlToJson', () => {
);
});

test.skip('xml declaration tag', () => {
test('xml declaration tag', () => {
const xml: string = `
<?xml version="1.0" encoding="UTF-8"?>
Expand Down
11 changes: 8 additions & 3 deletions src/helpers/inputConverters/xml/xmlToJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const parseXml = (xml: string) => {
const parseXmlWithXhtmlHandling = (xml: string): any => {
const options: Record<string, any> = {
ignoreAttributes: false,
ignoreDeclaration: true,
attributeNamePrefix: ATTRIBUTE_PREFIX,
allowBooleanAttributes: true,
alwaysCreateTextNode: true,
Expand Down Expand Up @@ -76,16 +77,20 @@ const getXhtmlPaths = (currentPath, currentValue, xhtmlPaths) => {

const standardizeJson = (json, rootKey: string): Record<string, any> => {
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;
Expand Down

0 comments on commit 99166ee

Please sign in to comment.