如何在XML中使用'和'搜索属性文本

问题描述

我正在使用Python和ElementTree

我有一个问题,我有2个xml文件在这里我需要将一些信息从一个复制到另一个。因此,我必须从第二个xml文件中的第一个xml文件搜索一个字符串 问题在于该字符串可以包含各种字符,还包括字符'和'

我写了以下搜索行:

slpRow = slpNode.find("row/[@id='%s']" % (tsSource))

在这种情况下,当tsSource包含字符'

时出现错误

我可以通过写来解决

slpRow = slpNode.find('row/[@id="%s"]' % (tsSource))

在这种情况下,但是当使用字符“

时会导致问题

所以我的问题是,当tsSource包含“以及”

有没有办法处理这种情况?

更改xml文件并使用&#39 resp。 &#34不是一种选择,因为文件自动生成的,并且这种合并只是链中的一步

谢谢

编辑: 为了清楚起见,所有内容都是翻译工具

一个XML文件中的XML字符串如下所示:

<tsSource>Peter said: "it is Frank's book""</tsSource>

在第二个XML中,类似

<row id="Peter said: "it is Frank's book"">
   <lang id="de_DE">Peter sagte: "Es ist Frank's Buch"</lang>
</row>

问题是,我必须在第二个文件搜索彼得说的字符串:“这是弗兰克的书”,但是要么

slpRow = slpNode.find('row/[@id="Peter said: "it is Frank's book""]')

slpRow = slpNode.find("row/[@id='Peter said: "it is Frank's book"']")

失败。

错误输出如下:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\python36_64\lib\xml\etree\ElementPath.py",line 263,in iterfind
    selector = _cache[cache_key]
KeyError: ('row/[@id="Peter said: "it is Frank\'s book""]',None)

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "D:/Tasks/59676 Automated translation tool/TranslationMerger.py",line 63,in <module>
    slpRow = slpNode.find('row/[@id="%s"]' % (tsSource))
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\python36_64\lib\xml\etree\ElementPath.py",line 298,in find
    return next(iterfind(elem,path,namespaces),None)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\python36_64\lib\xml\etree\ElementPath.py",line 277,in iterfind
    selector.append(ops[token[0]](next,token))
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\python36_64\lib\xml\etree\ElementPath.py",line 233,in prepare_predicate
    raise SyntaxError("invalid predicate")
SyntaxError: invalid predicate

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)