擅长:python、mysql、java
<p>我不确定我是否理解您的问题,但以下是我理解的部分(使用lxml和xpath):</p>
<pre><code>from lxml import etree
tree = etree.fromstring("""<foo>
<bar>
<unit>
<structure>
<token word="Rocky" att1="noun" att2="name"></token>
<token word="the" att1="article" att2=""></token>
<token word="yellow" att1="adjective" att2="color"></token>
<token word="dog" att1="noun" att2="animal"></token>
</structure>
</unit>
</bar>
</foo>""")
// get a list of all possible words, att1, att2:
listWord = tree.xpath("//token/@word")
listAtt1 = tree.xpath("//token/@att1")
listAtt2 = tree.xpath("//token/@att2")
// get all the tokens with att2="animal"
for token in tree.xpath('//token[@att2="animal"]'):
do_your_own_stuff()
</code></pre>