[4suite] get attribute value via XPath

Mike Brown mike at skew.org
Tue Jul 1 23:01:01 MDT 2008


7kkvhyf02 at sneakemail.com wrote:
> How do I get the attribute value via XPath?
> 
> attr = Evaluate( '//element/@attr1', contextNode=doc )[0].nodeValue

You need to use string(), an XPath function. Also you can save some
typing if your DOMs are 4Suite Domlettes...

attr = doc.xpath( 'string(//element/@attr1)' )

Beware ... //element selects possibly lots of elements throughout the 
document, and @attr1 is selecting each of their attr1 attribute nodes. 
string() operating on that node-set will show you the value of the first such 
attribute node, but it's wasteful to rely on that filter. It's better to try 
to make your selection as precise as possible and avoid //. If you only want 
the first "element" element's attr1 value, use something like 
string(/path/to/element[1]/@attr1).



More information about the 4suite mailing list