Home / Java Patterns and Pitfalls     frequal.com

Using a Variable in an XPath Attribute Specification

XPath allows you to select nodes based on the value of an attribute. When using XPath expressions from XSLT, you may want to use a variable within the attribute comparison string. At least with xsltproc, however, this is tricky. Simply putting the variable inside the attribute double-quotes doesn't seem to work. For example, to find all foo elements where the attribute bar is equal to the variable named variableName, the following doesn't work:


The fix is to construct the string using the concat function:
select="//foo[@bar=concat($variableName, '')]"
With xsltproc this alternative works nicely.

Last modified on 30 Aug 2005 by AO

Copyright © 2016 Andrew Oliver