I am using nodejs and typescript. It is necessary to get such an xml file structure from the object, in which the number of nested Rub tags is arbitrary:
<Doc file="jdsf35aasdg">
<Rub id="1" name="Руб1" />
<Rub id="2" name="Руб2" />
<Rub id="3" name="Руб3" />
</Doc>
The structure of the original object is not important to me, only the resulting xml. The closest result I got was with this initial object:
const doc = {
Doc : {
$ : {
"file" : "jdsf35aasdg"
},
Rub : {
$: {
id : "1",
name : "Руб1"
}
}
}
};
const builder = new xml2js.Builder({
headless: true
});
return builder.buildObject(object);
<Doc file="jdsf35aasdg">
<Rub id="1" name="Руб1"/>
</Doc>
But I could not set an array instead of one element - an error of the object structure. How do I need to set an object in order to get the result shown above as a result of the conversion to xml?
Thanks in advance!
If you want to construct XML then XSLT is a good choice, in Node.js and in client-side JavaScript you have XSLT 3.0 and XPath 3.1 support thanks to Saxon-JS (https://www.npmjs.com/package/saxon-js), so your example used in Node.js would look like
and output e.g.
You can also run it in the browser: