The raw XML text.
The slimmed chemical records.
When the XML is malformed.
parseOshaXml('<chemical_db><chemical><Analyte>ACETONE</Analyte><CASNumber>67-64-1</CASNumber></chemical></chemical_db>');
// [{ name: "ACETONE", cas: "67-64-1", synonyms: [] }]
export function parseOshaXml(xml: string): OshaChemical[] {
const doc = new DOMParser().parseFromString(xml, 'text/xml');
if (doc.getElementsByTagName('parsererror').length > 0) {
throw new Error('Malformed OSHA OBIS XML');
}
const chemicals: OshaChemical[] = [];
for (const node of Array.from(doc.getElementsByTagName('chemical'))) {
const analyte = readField(node, 'Analyte');
if (analyte === undefined) continue;
const chemical: OshaChemical = {
name: analyte.replace(/[†‡]/g, '').trim(),
synonyms: (readField(node, 'Synonyms') ?? '')
.split(';')
.map((synonym) => synonym.trim())
.filter((synonym) => synonym !== ''),
};
for (const [field, tag] of FIELD_MAP) {
const raw = readField(node, tag);
if (raw === undefined) continue;
// OBIS publishes links without a scheme.
const value = tag.endsWith('URL') && !/^https?:\/\//.test(raw) ? `https://${raw}` : raw;
Object.assign(chemical, { [field]: value });
}
chemicals.push(chemical);
}
return chemicals;
}
Parses the OBIS
Chemical_DB.xmldocument into slimmed records. Records without a name are skipped. RequiresDOMParser(available on extension pages, not in the service worker).