如何解决如何在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 举报,一经查实,本站将立刻删除。