1
我不知道是否可以使用lxml库中的tree.findall(“...”)语句来创建条件语句?使用lxml etree通过xml文件循环条件
我有一个文件
<sss version="1.2">
<date>2011-09-23</date>
<time>12:32:29</time>
<origin>OPST</origin>
<user></user>
<survey>
<name>Test</name>
<version>2011-09-02 15:50:10</version>
<record ident="A">
<variable ident="10" type="quantity">
<name>no_v</name>
<label>Another question</label>
<position start="23" finish="24"/>
<values>
<range from="0" to="32"/>
</values>
</variable>
<variable ident="11" type="quantity">
<name>v_683</name>
<label>another totally another Question</label>
<position start="25" finish="26"/>
<values>
<range from="0" to="33"/>
</values>
</variable>
<variable ident="12" type="quantity">
<name>v_684</name>
<label>And once more Question</label>
<position start="27" finish="29"/>
<values>
<range from="0" to="122"/>
</values>
</variable>
<variable ident="20" type="single">
<name>v_684</name>
<label>Question with alternatives</label>
<position start="73" finish="73"/>
<values>
<range from="1" to="6"/>
<value code="1">Alternative 1</value>
<value code="2">Alternative 2</value>
<value code="3">Alternative 3</value>
<value code="6">Alternative 4</value>
</values>
</variable>
</record>
</survey>
</sss>
我想现在要做的是让只有调查/记录/变量/名称的.text和调查/记录/变量/值/值以下XML结构。文本,如果名称以“V_”
开始到目前为止,我已经第一部分
from lxml import etree as ET
tree = ET.parse('scheme.xml')
[elem.text for elem in tree.getiterator(tag='name') if elem.text.startswith('v_')]
但我怎么能得到相同的元素的调查/记录/变量/值/值的.text ..并使用调查/重新线/变量/名称.text像过滤器? 非常感谢!
圣!这看起来相当先进...很感谢!任何人都有一个很好的链接到整个事情的介绍? – Jurudocs
@Jududocs:我从[本教程](http://www.w3schools.com/xpath/default.asp)学习了XPath,但请注意,该网站已经获得[一些批评](http://w3fools.com/) 。所以一旦你得到了基本的想法,依靠[specs](http://www.w3.org/TR/xpath/)作为权威资源。 – unutbu
Thanx,看起来像一个很好的介绍,以进一步解决这个问题...伟大的帮助! – Jurudocs