简体   繁体   English

在Python中使用XPATH访问子XML元素

[英]Accessing child XML elements with XPATH in Python

I'm parsing this XML file: https://www.dropbox.com/s/i6hga7nvmcd6rxx/ct.cps?dl=0 我正在解析此XML文件: https : //www.dropbox.com/s/i6hga7nvmcd6rxx/ct.cps?dl=0

From each <Reaction> tag I want its name attribute and the name attribute of its <Constant> children. 从每个<Reaction>标记中,我需要其name属性和其<Constant>子级的name属性。

from lxml import etree

NSMAP = {"c": "http://www.copasi.org/static/schema"}

parsed = etree.parse('ct.cps')

for a in parsed.xpath("//c:Reaction", namespaces=NSMAP):
    print a.attrib['name']

I can access each of the two elements' name attributes by using the above code. 通过使用上面的代码,我可以访问两个元素的name属性。 However, when I'm in one iteration of the <Reaction> elements, how could I then access subelements and list them out? 但是,当我在<Reaction>元素的一个迭代中时,如何访问子元素并将其列出?

I've tried this: 我已经试过了:

for a in parsed.xpath("//c:Reaction", namespaces=NSMAP):
    for b in a.xpath('Constant'):
        print b.attrib['name']

But it doesn't work. 但这是行不通的。

Here's a sample of the XML 这是XML的示例

</rdf:RDF>
        </MiriamAnnotation>
      </Metabolite>
    </ListOfMetabolites>
    <ListOfReactions>
      <Reaction key="Reaction_0" name="v1" reversible="false" fast="false">
        <MiriamAnnotation>
<rdf:RDF xmlns:dcterms="http://purl.org/dc/terms/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <rdf:Description rdf:about="#Reaction_0">
    <dcterms:created>
      <rdf:Description>
        <dcterms:W3CDTF>2015-06-16T22:13:07Z</dcterms:W3CDTF>
      </rdf:Description>
    </dcterms:created>
  </rdf:Description>
</rdf:RDF>
        </MiriamAnnotation>
        <ListOfSubstrates>
          <Substrate metabolite="Metabolite_5" stoichiometry="1"/>
        </ListOfSubstrates>
        <ListOfModifiers>
          <Modifier metabolite="Metabolite_9" stoichiometry="1"/>
        </ListOfModifiers>
        <ListOfConstants>
          <Constant key="Parameter_4344" name="Kcat" value="433.724"/>
          <Constant key="Parameter_4343" name="km" value="479.617"/>
        </ListOfConstants>
        <KineticLaw function="Function_40">
          <ListOfCallParameters>
            <CallParameter functionParameter="FunctionParameter_264">
              <SourceParameter reference="Parameter_4344"/>
            </CallParameter>
            <CallParameter functionParameter="FunctionParameter_254">
              <SourceParameter reference="Metabolite_9"/>
            </CallParameter>
            <CallParameter functionParameter="FunctionParameter_258">
              <SourceParameter reference="Metabolite_5"/>
            </CallParameter>
            <CallParameter functionParameter="FunctionParameter_266">
              <SourceParameter reference="Parameter_4343"/>
            </CallParameter>
          </ListOfCallParameters>
        </KineticLaw>
      </Reaction>
      <Reaction key="Reaction_1" name="v2" reversible="false" fast="false">
        <MiriamAnnotation>
<rdf:RDF xmlns:dcterms="http://purl.org/dc/terms/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <rdf:Description rdf:about="#Reaction_1">

When your parent element in an xml has a namespace, the child are also of same namespace (unless explicitely specified in the xml element), so when you are trying to search for them using XPATH , you will have to specify namespace, for children as well. 当xml中的父元素具有名称空间时,子元素也具有相同的名称空间(除非在xml元素中明确指定),因此,当您尝试使用XPATH搜索它们时,必须为子元素指定名称空间,好。

Try the following - 试试以下-

for a in parsed.xpath("//c:Reaction", namespaces=NSMAP):
    for b in a.xpath(".//c:Constant", namespaces=NSMAP):
        print b.attrib['name']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM