Home > Back-end >  In Node how to extract data from xml?
In Node how to extract data from xml?

Time:05-05

I am using libxmljs2 and I have this XML:

<Document xmlns="http://hl7.org/fhir">...

<address>
    <type value="both" />
    <line value="Avenue 123">
        <extension url="http://hl7.org/fhir/StructureDefinition/iso21090-ADXP-houseNumber">
            <valueString value="123" />
        </extension>
        <extension url="http://hl7.org/fhir/StructureDefinition/iso21090-ADXP-streetName">
            <valueString value="Avenue" />
        </extension>
    </line>
    <city value="Amsterdam" />
    <postalCode value="10623" />
</address>

and inside the address I can extract the line value like this:

    NAME_SPACE = { ns: 'http://hl7.org/fhir' }
    const myAddress = doc.get('//ns:Address/ns:line', NAME_SPACE) // Avenue 123

but how can I extract the extensions valueStrings separately?

something like:

const houseNumber = 
const streetName = 

CodePudding user response:

You just need to loop through the valueString items mapping the parent extension url component to its item value:

const NAME_SPACE = { ns: 'http://hl7.org/fhir' }
const myAddress = doc.get('//ns:Address/ns:line', NAME_SPACE)
const addressDetails = {}
myAddress.forEach(node => {
    const parentExtensionUrl = node.parent().attr('url').value();
    const addressDetailsItemName = parentExtensionUrl.substring(parentExtensionUrl.lastIndexOf('-')   1);
    addressDetails[addressDetailsItemName] = node.attr('value').value()
});

CodePudding user response:

I don't know Node but it looks as if you can use xpath queries to extract values from the xml. In xpath you can filter nodes by attribute.

For example for houseNumber:

//ns:Address/ns:line/ns:extension[@url='http://hl7.org/fhir/StructureDefinition/iso21090-ADXP-houseNumber']/ns:valueString/@value
  • Related