我有一个XML文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Reviews>
<Review rid="1004293">
<sentences>
<sentence id="1004293:0">
<text>Judging from previous posts this used to be a good place, but not any longer.</text>
<Opinions>
</sentence>
<sentence id="1004293:1">
<text>We, there were four of us, arrived at noon - the place was empty - and the staff acted like we were imposing on them and they were very rude.</text>
<Opinions>
</sentence>
<sentence id="1004293:2">
<text>They never brought us complimentary noodles, ignored repeated requests for sugar, and threw our dishes on the table.</text>
<Opinions>
<Opinion target="NULL" category="SERVICE#GENERAL" polarity="negative" from="0" to="0"/>
</Opinions>
</sentence>
</sentences>
</Review>
如何删除那些没有意见的句子?把那些句子放在文本有意见的地方? 我想得到这样的东西:
<sentences>
<sentence id="1004293:2">
<text>They never brought us complimentary noodles, ignored repeated requests for sugar, and threw our dishes on the table.</text>
<Opinions>
<Opinion target="NULL" category="SERVICE#GENERAL" polarity="negative" from="0" to="0"/>
</Opinions>
</sentence>
</sentences>
使用内置XML库(ElementTree)。你知道吗
注意:您发布的XML无效,我必须修复它。你知道吗
输出
我将使用这个模块将xml转换为dict,例如:How to convert an xml string to a dictionary?,过滤掉不需要的节点并重新转换为xml。。。。你知道吗
考虑使用XSLT,这是一种专门用于转换XML文档的语言。具体来说,先运行identity转换,然后在语句上运行一个空模板,并带有所需的条件。你知道吗
XSLT(另存为.xsl文件,一个特殊的.xml文件)
Online Demo
Python(使用第三方模块,
lxml
)相关问题 更多 >
编程相关推荐