tuananh/camaro

Array in an Array

warlord0 opened this issue · 3 comments

Describe the bug
I'm trying to template a file that could have an array of tags inside and array of tags. When I try parsing it the return is returned as [Array]

Minimal Script To Reproduce
This is the XML I'm using. It may have more than one ContactPostals each with AddressLine's within.

<ContactPostals xmlns="">
            <AddressID>1013923</AddressID>
            <AddressNumber>8</AddressNumber>
            <AddressLine>Any Close</AddressLine>
            <AddressLine>Anytown</AddressLine>
            <AddressLine>Anyshire</AddressLine>
            <AddressLine xsi:nil="true"/>
            <AddressLine xsi:nil="true"/>
            <AddressLine xsi:nil="true"/>
            <Postcode>LE44 4AA</Postcode>
</ContactPostals>

This is the template I'm using:

            contactPostals: [ '//ContactPostals', {
                addressNumber: AddressNumber',
                addressLine: [ 'AddressLine', '.' ],
                postcode: 'Postcode',
                uPRN: 'UPRN'
            },

Which yields

contactPostals: 
   [ { addressNumber: '8',
       addressLine: [Array],
       postcode: 'LE44 4AA',
       uPRN: '' } ],

Expected Results

addressLine: [
  '8', 'Any Close', 'Anytown', 'Anyshire', '', ''
]
  • camaro version: 4.0.5
  • Node version: 8.16.0
  • Operating system: Debian Buster

Further info:

If I don't use an array at the ContactPostals level I get the desired result.

contactPostals: {
    addressNumber: '//ContactPostals/AddressNumber',
    addressLine: [ '//ContactPostals/AddressLine[text() and not(text()=\'-\')]', '.' ],
    postcode: '//ContactPostals/Postcode',
    uPRN: '//ContactPostals/UPRN'
},

Rresult.

  contactPostals: 
   { addressNumber: '8',
     addressLine: [ 'Any Close', 'Anytown', 'Anyshire ' ],
     postcode: 'LE44 4AA',
     uPRN: '' }

i use this snippet and it seems to work for me

const { transform } = require('camaro')

const xml = `
    <ContactPostals xmlns="">
            <AddressID>1013923</AddressID>
            <AddressNumber>8</AddressNumber>
            <AddressLine>Any Close</AddressLine>
            <AddressLine>Anytown</AddressLine>
            <AddressLine>Anyshire</AddressLine>
            <AddressLine xsi:nil="true"/>
            <AddressLine xsi:nil="true"/>
            <AddressLine xsi:nil="true"/>
            <Postcode>LE44 4AA</Postcode>
    </ContactPostals>
`

const template = {
    contactPostals: [ '//ContactPostals', {
                addressNumber: 'AddressNumber',
                addressLine: [ 'AddressLine[text() and not(text()=\"-\")]', '.' ],
                postcode: 'Postcode',
                uPRN: 'UPRN'
            }]
}

;(async function () {
    const output = await transform(xml, template)
    console.log(JSON.stringify(output, null, 4))
})();

output

{
    "contactPostals": [
        {
            "addressNumber": "8",
            "addressLine": [
                "Any Close",
                "Anytown",
                "Anyshire"
            ],
            "postcode": "LE44 4AA",
            "uPRN": ""
        }
    ]
}

probably just the way you print out the result maybe? try JSON.stringify(output, null, 4) to see if you get what you need.