Xpath local-name() 中的属性 [英] Attributes in Xpath local-name()
问题描述
这是我的 xml 文件的一个小示例.
This is a small sample of my xml file.
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:pPr>
<w:rPr>
<w:highlight w:val="yellow"/>
</w:rPr>
</w:pPr>
<w:bookmarkStart w:id="0" w:name="_GoBack"/>
<w:bookmarkEnd w:id="0"/>
<w:r w:rsidRPr="00D1434D">
<w:rPr>
<w:rFonts w:ascii="Times New Roman"
w:eastAsia="MS PGothic"
w:hAnsi="Times New Roman"/>
<w:b/>
<w:color w:val="000000"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
<w:highlight w:val="yellow"/>
</w:rPr>
<w:t xml:space="preserve">Responses to </w:t>
</w:r>
<w:r w:rsidR="00335D4A" w:rsidRPr="00D1434D">
<w:rPr>
<w:rFonts w:ascii="Times New Roman"
w:eastAsia="MS PGothic"
w:hAnsi="Times New Roman"/>
<w:b/>
<w:color w:val="000000"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
<w:highlight w:val="yellow"/>
<w:lang w:eastAsia="ja-JP"/>
</w:rPr>
<w:t>the Reviewer</w:t>
</w:r>
</w:p>
我想使用 w:highlight
标记提取文本,特别是具有属性 value
= "yellow" .我搜索了它,但无法提出解决方案.
I want to extract text with the w:highlight
tag specifically having the attribute value
= "yellow" . I searched for it but wasn't able to come up with a solution.
以下适用于一般突出显示:
The following works for highlight in general:
for t in source.xpath('.//*[local-name()="highlight"]/../..//*[local-name()="t"]'):
do something
我试过了:
for t in lxml_tree.xpath('//*[local-name()="highlight"][@val="yellow"]/../..//*[local-name()="t"]'):
这不起作用,什么都不返回..
this doesn't work, returns nothing..
推荐答案
w:val
属性在命名空间中,所以你不能只通过 @val
解决它.一种可能的解决方案是使用 @*[local-name()='attribute name']
表达式通过属性的本地名称来寻址属性,类似于您对元素所做的:
w:val
attribute is in namespace, so you can't just address it by @val
. One possible solution is by using @*[local-name()='attribute name']
expression to address an attribute by it's local name, similar to what you've done for elements :
//*[local-name()="highlight"][@*[local-name()='val' and .='yellow']]/../..//*[local-name()="t"]
这篇关于Xpath local-name() 中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!