I am trying to parse XML files in nestjs using xml2js an everything worked fine until now, but now i've got a problem:
XML to parse:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="example_text">
Hi there MR. <xliff:g id="mister" example="John">%1$s</xliff:g> and Mrs. <xliff:g id="miss" example="Jessica">%1$s</xliff:g>
</string>
</resources>
Usage of xml2js in code:
const xmlParser = require('xml2js').Parser({trim: true, ignoreAttrs: false, mergeAttrs: true});
...
const xml = entry.getData().toString('utf8');
xmlParser.parseString(xml, function(err, result) {
console.log('Result: ', result);
if (result && result.resources.string) {
const strings = result.resources.string;
strings.map(x => {
console.log('String: ', x);
});
}
});
Output:
Result: { resources:
{ 'xmlns:xliff': [ 'urn:oasis:names:tc:xliff:document:1.2' ],
string: [ [Object] ] } }
String: { _: 'Hi there MR. and Mrs.',
name: [ 'example_text' ],
'xliff:g':
[ { _: '%1$s', id: [Array], example: [Array] },
{ _: '%1$s', id: [Array], example: [Array] } ] }
And now my problem:
- I want to know where the xliff Tags are located inside the string "Hi There MR. %1$s and Mrs.%1$s"
- The String does not even include "%1$s" or any information regarding the xliff tags
Why i need the information:
- i want to replace %1$s with something else and store the information included in the xliff
Is there a way to do this with xml2js, or do i need another library to parse the xml, in order to get the needed information?