使用节点XML解析器解析XLIFF

问题描述

我正在尝试使用xml2js库解析XLIFF文件。一切正常,但是如果我有类似的内容<source>Welcome to <x id="INTERPOLATION" equiv-text="{{ title }}"/> my friend</source>,我会得到[{"_":"Welcome to my friend","x":[{"$":{"id":"INTERPOLATION","equiv-text":"{{ title }}"}}]}]。我基本上是在减少句子部分的顺序。我希望得到一个由3部分组成的阵列:

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

但是我却得到了:

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

如果我尝试再次重新创建字符串,我会得到<source>Welcome to my friend<x id="INTERPOLATION" equiv-text="{{ title }}"/></source>

有什么想法如何使用此XML解析器或其他解析器解决它吗?

解决方法

您可能还会喜欢txml。当像txml.parse(yourXMLString)这样使用它时,您将得到一个这样的对象:

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

我认为它绝对符合您的需求。 source中的三个孩子非常干净。而且,此解析器只有4kb的大小,并且不需要进行本机c编译,否则将在其他体系结构上运行您的应用程序时造成麻烦。

免责声明:我是txml的作者,这种观点可能不是客观的;-)