Por:¿Cómo extraer atributos XML usando Python elementtree
<foo>
<bar key="value">text</bar>
</foo>
¿Cómo llego "valor"?
xml.findtext("./bar[@key]")
Lanza un error.
Por:¿Cómo extraer atributos XML usando Python elementtree
<foo>
<bar key="value">text</bar>
</foo>
¿Cómo llego "valor"?
xml.findtext("./bar[@key]")
Lanza un error.
Encontrará la primera instancia de un elemento denominado bar
y devolverá el valor del atributo key
.
In [52]: import xml.etree.ElementTree as ET
In [53]: xml=ET.fromstring(contents)
In [54]: xml.find('./bar').attrib['key']
Out[54]: 'value'
Su expresión:
./bar[@key]
Esto significa: bar
niños que tienen key
atributo
Si desea seleccionar el atributo, utilice esta expresión relativa:
bar/@key
Es significa: el atributo key
de los niños bar
Por supuesto, debe tener en cuenta al utilizar un motor de XPath totalmente compatible como lxml.
No estoy seguro si es ElementTree o Google App Engine pero el uso de '@' plantea SyntaxError ("sintaxis de ruta no soportada (% s)"% op) SyntaxError: sintaxis de ruta no soportada (@) –
@Will Merydith: Por favor, lea mi última frase. La API básica de 'ElementTree' no es una queja completa del motor XPath ... –
OK. Veré si puedo encontrar un módulo que funcione en GAE/Py2.5.5. –
¿Cuál es el mensaje de error? – keegan3d