I don't think I have understood your question fully.
Is the problem that $element holds an element name, and you want to select child different elements depending on the name? In that case you need something like this:
declare variable $element as xs:QName external;
.... where $r2/*[node-name() = $element] = $r1/Number
and then you can bind a value from the XQJ application as
xqExpr.bindItem(new QName("element"), new QName("some-element-name"))
Michael Kay

From: Erik Rens [mailto:mail2rens@gmail.com]
Sent: 02 April 2009 11:50
To: Mailing list for the SAXON XSLT and XQuery processor
Subject: Re: [saxon] Fwd: Xpath 2.0 sequences and Saxon examples problems.

How can I use element or node variabele in my xquery string in  Java ?
The reason why is to build my query string dynamically. I know how to use string or integer variables. But voor element selection is have to use another i guess. So I want to replace "$element" by de tag element. If i use string variable it wont work ?

xquerystring code :

+ "declare variable $xmlDoc2 as node() external; "
            + "declare variable $getal as xs:integer external;"
            + "declare variable $matchingElement1 as element() external;"
            + "for $r1 in $xmlDoc1/ROOT/RECORD, "
            + "$r2 in $xmlDoc2/ROOT/RECORD "
            + "where $r2/$element = $r1/Number and $r2/Age > $getal"
            + " return "
            + "<ROOT><RECORD>{$r1/*, $r2/(* except ID)}</RECORD></ROOT>";

binding code:
XQExpression xqExpr = conn.createExpression();
            xqExpr.bindItem(new QName("xmlDoc1"), xqItem1);
            xqExpr.bindItem(new QName("xmlDoc2"), xqItem2);

Does somebody know ?