简体   繁体   中英

Why I can't use Xpath `.//node-name` to access a node?

The xml file is like this:

<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="Cistrome.xsl"?>
<motifs>
  <motif id="hPDI060">
  ..........
  </motif>
</motifs>

My python code is like this:

    tree = ElementTree.parse(sys.argv[1])
    for node in tree.findall('.//motifs'):
        print("found")

However, after I run the codes, the found string is not displayed, in other words, .//motifs doesn't find the right tag.

Does anyone have ideas about this? Thanks!

findall will look for all the child elements of the current tag, while your current tag is "motifs". Therefore, there is nothing found

You can check what the current tag is by

> tree.tag
> 'motifs'

Be sure with what you wanna find, motifs or motif

tree.findall('*')将在根元素motifs下找到所有motif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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