Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在xml中按字段内容拆分条目_Xml - Fatal编程技术网

在xml中按字段内容拆分条目

在xml中按字段内容拆分条目,xml,Xml,在一次数据输入错误之后,我得到了有点像这样的XML: <lift> <header> ... </header> <entry> <lexical-unit> <form lang="grt"><text>Apdala /Apanga</text></form> </lexical-unit> <trait name="mor

在一次数据输入错误之后,我得到了有点像这样的XML:

<lift>
  <header> ... </header>
  <entry>
    <lexical-unit>
      <form lang="grt"><text>Apdala /Apanga</text></form>
    </lexical-unit>
    <trait  name="morph-type" value="phrase"/>
    <sense>
      <definition>
        <form lang="hi"><text>स्वयं</text></form>
      </definition>
    </sense>
  </entry>
  <entry>
    ...
  </entry>
  ⋮
</lift>

... 
阿普达拉/阿潘加
स्वयं
...
⋮
如果
下的
文本
元素中有正斜杠,我想在斜杠上拆分此字段(并修剪空白),并创建一个全新的
条目
元素,复制条目元素的其余内容,但在上述情况下,
Apalda
作为一个条目的文本,而
Apanga
作为另一个条目的文本。可能没有正斜杠,在这种情况下保留条目不变,或者有多个条目,在这种情况下,为该字符串的每个标记创建一个新的
条目


我不介意我使用什么语言或脚本来完成这项工作,但最好不要下载任何大型框架等。这是一次性的工作,不必再这样做。我使用的是Ubuntu 14.04,并且已经安装了常用的开发工具。

以下是python的答案

import xml.etree.ElementTree as etree
from copy import deepcopy
tree = etree.parse('my xml file')
root = tree.getroot()
entries = root.findall('entry')
for entry in entries:
    problem_string = entry.find('lexical-unit').find('form').find('text').text
    tokens = problem_string.split('/')
    for token in tokens:
        entry.find('lexical-unit').find('form').find('text').text = token.strip()
        if token != tokens[-1]:
            entry = deepcopy(entry)
            root.append(entry)
tree.write('my parsed xml file', 'UTF-8')

它定位问题字符串并在斜杠上拆分,然后,对于每个标记,将其剥离并插入条目中的同一位置,在条目运行时创建条目的深度副本。

感谢您指出@PaulCrovella。你是对的,当然,我有点懒,想用它作为捷径。我现在已经用Python解决了这个问题,并在这里发布了我的答案。