In python: use xpath with an element from a list
Pergunta
With the help from several people here at Stackoverflow I came up with the following code:
import lxml.etree
f = open("PaVE.tre.xml", "r")
out = open("PaVE.2.xml", "a")
data = f.read()
line=["HPV16","Alpha"]
tree = lxml.etree.XML(data)
nsmap = {'phylo': 'http://www.phyloxml.org'}
matches = tree.xpath('//phylo:name[text()="HPV16"]', namespaces=nsmap)
for e in matches:
#do something fun
However, I am hard-coding the HPV16 into the xpath expression. I would like to take the HPV16 from line[0]. I was thinking something like:
matches = tree.xpath('//phylo:name[text()='+line[0]+']', namespaces=nsmap)
However, that does not seem to work! As always, any help would be appreciated
EDIT: I added a couple of lines from the xml file as was requested:
<phyloxml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.phyloxml.org" xsi:schemaLocation="http://www.phyloxml.org http://www.phyloxml.org/1.10/phyloxml.xsd">
<phylogeny rooted="true">
<clade>
<clade>
<branch_length>0.5</branch_length>
<clade>
<name>HPV16</name>
<branch_length>1.0</branch_length>
</clade>
<clade>
Solução
You forgot the quotes in your XPath expression:
>>> '//phylo:name[text()='+line[0]+']'
'//phylo:name[text()=something]'
I would do this instead:
>>> '//phylo:name[text()="%s"]' % line[0]
'//phylo:name[text()="something"]'
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow