python – 当前节点的Xpath选择属性?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 当前节点的Xpath选择属性?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3473字,纯文字阅读大概需要5分钟。
内容图文
![python – 当前节点的Xpath选择属性?](/upload/InfoBanner/zyjiaocheng/791/62aa74460fd9440192cc5edeadfe90fd.jpg)
我使用python和lxml来处理xml.我查询/过滤后找到我想要的节点,但我遇到了一些问题.如何通过xpath获取其属性的值?这是我的输入示例.
>print(etree.tostring(node, pretty_print=True ))
<rdf:li xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" rdf:resource="urn:miriam:obo.chebi:CHEBI%3A37671"/>
我想要的值是在resource = ….目前我只是使用lxml来获取值.我想知道是否可以在纯xpath中进行?谢谢
编辑:忘了说,这不是根节点,所以我不能在这里使用//.我在xml文件中喜欢2000-3000个其他人.我的第一次尝试是使用“.@ attrib”和“self :: * @”,但这些似乎不起作用.
编辑2:我会尽力解释(好吧,这是我第一次使用xpath处理xml问题.而英语不是我最喜欢的字段之一……).这是我的输入代码片段http://pastebin.com/kZmVdbQQ(完整的一个从这里http://www.comp-sys-bio.org/yeastnet/使用版本4).
在我的代码中,我尝试使用资源链接chebi获取speciesTypes节点(< rdf:li rdf:resource =“urn:miriam:obo.chebi:....”/>).然后我试图从rdf:li中的rdf:resource属性获取值.问题是,我很确定如果我从诸如speciesTypes之类的父节点开始,在子节点中获取属性会很容易,但我想知道如果从rdf:li开始怎么做.根据我的理解,xpath中的“//”将从任何地方查找节点,而不仅仅是在当前节点中.
下面是我的代码
import lxml.etree as etree
tree = etree.parse("yeast_4.02.xml")
root = tree.getroot()
ns = {"sbml": "http://www.sbml.org/sbml/level2/version4",
"rdf":"http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"body":"http://www.w3.org/1999/xhtml",
"re": "http://exslt.org/regular-expressions"
}
#good enough for now
maybemeta = root.xpath("//sbml:speciesType[descendant::rdf:li[starts-with(@rdf:resource, 'urn:miriam:obo.chebi') and not(starts-with(@rdf:resource, 'urn:miriam:uniprot'))]]", namespaces = ns)
def extract_name_and_chebi(node):
name = node.attrib['name']
chebies = node.xpath("./sbml:annotation//rdf:li[starts-with(@rdf:resource, 'urn:miriam:obo.chebi') and not(starts-with(@rdf:resource, 'urn:miriam:uniprot'))]", namespaces=ns) #get all rdf:li node with chebi resource
assert len(chebies) == 1
#my current solution to get rdf:resource value from rdf:li node
rdfNS = "{" + ns.get('rdf') + "}"
chebi = chebies[0].attrib[rdfNS + 'resource']
#do protein later
return (name, chebi)
metaWithChebi = map(extract_name_and_chebi, maybemeta)
fo = open("metabolites.txt", "w")
for name, chebi in metaWithChebi:
fo.write("{0}\t{1}\n".format(name, chebi))
解决方法:
在XPath查询中使用@前缀属性名称:
>>> from lxml import etree
>>> xml = """... <?xml version="1.0" encoding="utf8"?>
... <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
... <rdf:li rdf:resource="urn:miriam:obo.chebi:CHEBI%3A37671"/>
... </rdf:RDF>
... """
>>> tree = etree.fromstring(xml)
>>> ns = {'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'}
>>> tree.xpath('//rdf:li/@rdf:resource', namespaces=ns)
['urn:miriam:obo.chebi:CHEBI%3A37671']
编辑
这是问题中脚本的修订版本:
import lxml.etree as etree
ns = {
'sbml': 'http://www.sbml.org/sbml/level2/version4',
'rdf':'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'body':'http://www.w3.org/1999/xhtml',
're': 'http://exslt.org/regular-expressions',
}
def extract_name_and_chebi(node):
chebies = node.xpath("""
.//rdf:li[
starts-with(@rdf:resource, 'urn:miriam:obo.chebi')
]/@rdf:resource
""", namespaces=ns)
return node.attrib['name'], chebies[0]
with open('yeast_4.02.xml') as xml:
tree = etree.parse(xml)
maybemeta = tree.xpath("""
//sbml:speciesType[descendant::rdf:li[
starts-with(@rdf:resource, 'urn:miriam:obo.chebi')]]
""", namespaces = ns)
with open('metabolites.txt', 'w') as output:
for node in maybemeta:
output.write('%s\t%s\n' % extract_name_and_chebi(node))
内容总结
以上是互联网集市为您收集整理的python – 当前节点的Xpath选择属性?全部内容,希望文章能够帮你解决python – 当前节点的Xpath选择属性?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。