2011-06-21 80 views
2

对于给定的元素,我想检查xsi:nil属性是否设置为true。etree获取属性作为值而不是字符串

我当前的代码是

xsinil = dataFact.get('{http://www.w3.org/2001/XMLSchema-instance}nil', False) 

但不是被True XSINIL为字符串类型...

什么是最好的解决方案?我不认为这是非常优雅:

xsinil=dataFact.get('{http://www.w3.org/2001/XMLSchema-instance}nil', False) 
if xsinil == 'true' or xsinil == '1' : 
    xsinil = True 

回答

1

这看起来更好:

xsinil = dataFact.get('...', False) in ('true', '1') 

它指定Truexsinil变量只有get功能的结果是True'true''1'之一。

+0

-1'Element.get(属性名称,假)'将永远不会返回TRUE; –

+0

同意,最终的答案将是'XSINIL = dataFact.get(” ......')in('true','1')' – rds

+0

@rds:所以不接受这个答案并接受我的。 –

1

Element.get()的第二个参数几乎无关紧要 - 只是不要使用True

所有你需要的是:

xsinil = dataFact.get('......') in ('true', '1') 
相关问题