Xpath lxml中的hasAttribute?
下面是我用于解析xml文件的路径。我正在使用Xpath lxml中的hasAttribute?,xpath,python-2.7,attributes,lxml,Xpath,Python 2.7,Attributes,Lxml,下面是我用于解析xml文件的路径。我正在使用x.hasAttribute('marL')==True:使用ElementTree但是我不知道如何在lxml中使用hasAttribute来检查p:ph是否包含名为marL的属性。我尝试了上面的方法,但效果不佳,而且在lxml示例中也没有找到。任何人都可以建议lxml中用于hasAttribute的函数是什么,或者在上述实例中使用它吗 任何帮助都将不胜感激 试试看 path4 = file.xpath('/p:sld/p:cSld/p:spTree/
x.hasAttribute('marL')==True:
使用ElementTree
但是我不知道如何在lxml
中使用hasAttribute
来检查p:ph
是否包含名为marL
的属性。我尝试了上面的方法,但效果不佳,而且在lxml示例中也没有找到。任何人都可以建议lxml中用于hasAttribute
的函数是什么,或者在上述实例中使用它吗
任何帮助都将不胜感激 试试看
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'})
试一试
属性exist的xpath谓词很简单:
[@marL]
因此,请尝试:
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'})
或:
有关谓词的更多信息,请参见示例。“
employee[@secretary and@assistant]
选择上下文节点中同时具有secretary属性和assistant属性的所有员工子节点”属性exist的xpath谓词很简单:[@marL]
因此,请尝试:
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'})
或:
有关谓词的更多信息,请参见示例。“
employee[@secretary and@assistant]
选择上下文节点中同时具有secretary属性和assistant属性的所有员工子节点”这很完美,但是如何检查p:ph是否具有属性type、quarter而不是marl i、e x.hasAttribute('marl')==False。我需要使用此条件从标记中获取不同的文本。我试过@marL=False哪种力有效!对不起,我不明白检查“是否p:ph”。Xaph例如:'//p:ph[@type=“body”和@sz=“quarter”和@marL]
将返回谓词中满足“过滤条件”的所有p:ph。这是正确的,但我想检查p:ph
标记是否具有属性@type=“body”
和@sz=“quarter“
并检查它是否包含名为@marL
的属性。如果是真的,或者如果p:ph没有属性marL,则为假。我希望它现在更令人信服!为了使它更简单,这是一行使用ElementTree的代码,如果all10.nodeName='a:pPr'和all10.getAttribute('lvl')='2'和all10.hasAttribute('marL')==False:
,我想在lxml
中重写它<代码>http://stackoverflow.com/questions/16912361/rewrite-elementtree-code-in-lxml查看此文件了解更多详细信息。谢谢!这很完美,但是如何检查p:ph是否有属性type,quarter而不是marl i,ex.hasaAttribute('marl')==False。我需要使用此条件从标记中获取不同的文本。我试过@marL=False哪种力有效!对不起,我不明白检查“是否p:ph”。Xaph例如:'//p:ph[@type=“body”和@sz=“quarter”和@marL]
将返回谓词中满足“过滤条件”的所有p:ph。这是正确的,但我想检查p:ph
标记是否具有属性@type=“body”
和@sz=“quarter“
并检查它是否包含名为@marL
的属性。如果是真的,或者如果p:ph没有属性marL,则为假。我希望它现在更令人信服!为了使它更简单,这是一行使用ElementTree的代码,如果all10.nodeName='a:pPr'和all10.getAttribute('lvl')='2'和all10.hasAttribute('marL')==False:
,我想在lxml
中重写它<代码>http://stackoverflow.com/questions/16912361/rewrite-elementtree-code-in-lxml查看此文件了解更多详细信息。谢谢!
'/p:sld/p:cSld/p:spTree/p:sp/p:nvSpPr/p:nvPr/p:ph[@type="body" and @sz="quarter" and @marL]'