Originally posted by Ingo Schildmann:
That is no mystery. You just can't replace the
NCName that is required as nametest in the step with $Lang's string. Think of them as different data types.
Mystery for me lies in "why", in this case "why not"?
$Lang is an expression, and expressions are allowed in XPath steps. "name()=$Lang" construction has almost the same meaning, it is simply written differently. The only difference is that $Lang expression could potentially span hierarchical levels, when name() refers to the current level only. XSLT processor is perfectly capable to evaluate $Lang expression, it does it in attribute-value templates. We can write <xsl:element name="{$Lang}"/> for example. It just doesn't convert Strings to nodes and "live" XPath expressions.
This absence of dynamic evaluation features is unusual for "small" languages, and this is why I called it "mystery".
The best explanation I could find says something vague about "implications on the run-time architecture of the processor" and hints that this could hamper processor's ability to do static optimization:
"14.5 Dynamic XPath Expressions
In certain circumstances, it is useful in a stylesheet to evaluate XPath expressions that are not hard-coded in the stylesheet text. For example, XPath expressions may be read from the source document, or supplied to the stylesheet as parameters, or constructed as a string from run-time information. A common requirement is for an application to allow the user to select the key that should be used for sorting output, and a natural mechanism for implementing this is to allow the sort key to be passed to the stylesheet as a string parameter, with the string containing an XPath expression to be used as the sort key.
Issue (evaluate-function): There is at present no consensus within the working group that such a function should be provided, as it has significant implications on the run-time architecture of the processor, as well as the ability to do static optimization."
XSL Transformations (XSLT) Version 2.0 (Working Draft) Maybe "static optimization" is the explanation.