我有以下XML元素:
<AdditionalAttribute name="Internal Version Identifier" value="UCV261KSBQGBPYF6" dataType="String"></AdditionalAttribute>
这些标签有很多,我想具体针对这个标签。一些标签的例子如下:
<AdditionalAttribute name="Version Notes" value="This Item was released indirectly as a result of releasing Item 10X9061-11 to In Design." dataType="String"></AdditionalAttribute>
<AdditionalAttribute name="Change Number" value="BCV2386079506" dataType="String"></AdditionalAttribute>
<AdditionalAttribute name="Version Shared" value="No" dataType="String"></AdditionalAttribute>
<AdditionalAttribute name="Effective Version Shared" value="No" dataType="String"></AdditionalAttribute>
<AdditionalAttribute name="Material Effectivity Date" value="2016-03-15T08:52:33-08:00" dataType="String"></AdditionalAttribute>
<AdditionalAttribute name="Internal Version Identifier" value="UCV261KSBQGBPYF6" dataType="String"></AdditionalAttribute>
<AdditionalAttribute name="Item Creation Date" value="2016-03-01T09:08:14-08:00" dataType="String"></AdditionalAttribute>
我使用lxml和xpath尝试查询“内部版本标识符”的所有其他属性,一旦我有了需要从'value'字段中提取值的元素。
我想找到名称=“内部版本标识符”的AdditionalAttribute元素,然后在“value”字段中获取值。
我一直在尝试一些不同的东西但却无法获得我需要的价值:
这是我当前的xpath查询:
tree.xpath('//AdditionalAttribute[starts-with(text(), "Internal")]')
答案 0 :(得分:2)
尝试//AdditionalAttribute[@name="Internal Version Identifier"]
。
经过测试的例子:
from lxml import etree
tree = etree.XML('''
<root>
<AdditionalAttribute name="Version Notes" value="This Item was released indirectly as a result of releasing Item 10X9061-11 to In Design." dataType="String"></AdditionalAttribute>
<AdditionalAttribute name="Change Number" value="BCV2386079506" dataType="String"></AdditionalAttribute>
<AdditionalAttribute name="Version Shared" value="No" dataType="String"></AdditionalAttribute>
<AdditionalAttribute name="Effective Version Shared" value="No" dataType="String"></AdditionalAttribute>
<AdditionalAttribute name="Material Effectivity Date" value="2016-03-15T08:52:33-08:00" dataType="String"></AdditionalAttribute>
<AdditionalAttribute name="Internal Version Identifier" value="UCV261KSBQGBPYF6" dataType="String"></AdditionalAttribute>
<AdditionalAttribute name="Item Creation Date" value="2016-03-01T09:08:14-08:00" dataType="String"></AdditionalAttribute>
</root>
''')
elements = tree.xpath('//AdditionalAttribute[@name="Internal Version Identifier"]')
assert len(elements) == 1
assert elements[0].attrib["value"] == 'UCV261KSBQGBPYF6'
或者,如果您只对value
而不是整个元素感兴趣:
elements = tree.xpath(
'//AdditionalAttribute[@name="Internal Version Identifier"]/@value')
assert len(elements) == 1
assert elements[0] == 'UCV261KSBQGBPYF6'
答案 1 :(得分:0)
您可以使用://AdditionalAttribute/@value
;
它会抓住:value="UCV261KSBQGBPYF6"