Issue parsing XLIFF with node XML parser

590 views Asked by At

Im trying to parse XLIFF file using xml2js library. All is working fine but if I have something like that: <source>Welcome to <x id="INTERPOLATION" equiv-text="{{ title }}"/> my friend</source> I will get [{"_":"Welcome to my friend","x":[{"$":{"id":"INTERPOLATION","equiv-text":"{{ title }}"}}]}]. I am basically loosing order for the parts of the sentence. I would expect to get an array of 3 parts:

"Welcome to "
[{"$":{"id":"INTERPOLATION","equiv-text":"{{ title }}"}}]
" my friend"

But instead Im getting:

"Welcome to my friend"
[{"$":{"id":"INTERPOLATION","equiv-text":"{{ title }}"}}]

If I would try to recreate string again I would get <source>Welcome to my friend<x id="INTERPOLATION" equiv-text="{{ title }}"/></source>

Any idea how to solve it with this XML parser or any other?

2

There are 2 answers

0
Tobias Nickel On

you also might like txml. When using it like txml.parse(yourXMLString), you get an object like this:

[
  {
    "tagName": "source",
    "attributes": {},
    "children": [
      "Welcome to ",
      {
        "tagName": "x",
        "attributes": {
          "id": "INTERPOLATION",
          "equiv-text": "{{ title }}"
        },
        "children": []
      },
      " my friend"
    ]
  }
]

I think it looks absolutely as what you are looking for. The three children inside the source, are very clean to use. Also, this parser is only 4kb in size and there is no need for native c compiling that will cause difficulties when running your app on a different architecture.

Disclaimer: I am the author of txml, and this opinion might not be objective ;-)

0
feech On

With fast-xml-parser. Please, use stopNodes in options when you parse the source. It makes fast-xml to treat the content as plain-strings

var parser = require("fast-xml-parser");
parser.parse(srcFile, {ignoreAttributes: false, stopNodes: ["source", "target"],});