我使用 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::*@”,但这些似乎不起作用。
EDIT2:我会尽力解释(好吧,这是我第一次使用 xpath 处理 xml 问题。英语不是我最喜欢的领域之一....)。这是我的输入片段 http://pastebin.com/kZmVdbQQ (完整的来自这里 http://www.comp-sys-bio.org/yeastnet/ 使用版本 4)。
在我的代码中,我尝试获取带有资源链接 chebi ( <rdf:li rdf:resource="urn:miriam:obo.chebi:...."/>)
的 speciesTypes 节点。然后我尝试从 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 选择属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8409306/