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