lxml中的hasAttribute?

时间:2013-06-03 12:38:45

标签: xpath python-2.7 attributes lxml

path4 = file.xpath('/p:sld/p:cSld/p:spTree/p:sp/p:nvSpPr/p:nvPr/p:ph[@type="body"][@sz="quarter"][@marL=True]', namespaces={'p':'http://schemas.openxmlformats.org/presentationml/2006/main',
            'a':'http://schemas.openxmlformats.org/drawingml/2006/main'})

这是我用于解析的xml文件的路径。我使用x.hasAttribute('marL') == True:使用ElementTree,但我不知道如何在hasAttribute中使用lxml来检查p:ph是否包含名为marL的属性。我尝试了上面但是dint工作,我也没有在lxml示例中找到它。 any1可以建议hasAttribute的lxml中的函数是什么,或者对于上面的实例使用它是什么?

任何帮助都将不胜感激!!

2 个答案:

答案 0 :(得分:1)

属性的xpath谓词存在很简单:[@marL] 因此,试试:

'/p:sld/p:cSld/p:spTree/p:sp/p:nvSpPr/p:nvPr/p:ph[@type="body"][@sz="quarter"][@marL]'

或:

'/p:sld/p:cSld/p:spTree/p:sp/p:nvSpPr/p:nvPr/p:ph[@type="body" and  @sz="quarter"  and  @marL]'

有关谓词的更多信息,请查看示例here employee[@secretary and @assistant]选择具有秘书属性和助手属性的上下文节点的所有员工子女”

答案 1 :(得分:0)

尝试

path4 = file.xpath('boolean(/p:sld/p:cSld/p:spTree/p:sp/p:nvSpPr/p:nvPr/p:ph[@type="body"][@sz="quarter"]/@marL)', namespaces={'p':'http://schemas.openxmlformats.org/presentationml/2006/main',
            'a':'http://schemas.openxmlformats.org/drawingml/2006/main'})
相关问题