Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xpath lxml中的hasAttribute?_Xpath_Python 2.7_Attributes_Lxml - Fatal编程技术网

Xpath lxml中的hasAttribute?

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/

下面是我用于解析xml文件的路径。我正在使用
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]'