Yes, use something of this form:
<xsl:apply-templates select="example">
<xsl:sort select="*[name()=sortby]" data-type="{sort-data-type}"
order="{sort-order}" />
</xsl:apply-templates>
Do note that you need to also specify the sort data-type and order.
Complete example:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="example">
<xsl:sort select="*[name()=/*/sortby]" data-type="{/*/sort-data-type}"
order="{/*/sort-order}" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="example">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the following XML document:
<examples>
<sortby>year</sortby>
<sort-data-type>number</sort-data-type>
<sort-order>descending</sort-order>
<example>
<year>2008</year>
<number>3</number>
</example>
<example>
<year>2012</year>
<number>2</number>
</example>
<example>
<year>2010</year>
<number>5</number>
</example>
</examples>
the wanted, correct result is produced:
<examples>
<example>
<year>2012</year>
<number>2</number>
</example>
<example>
<year>2010</year>
<number>5</number>
</example>
<example>
<year>2008</year>
<number>3</number>
</example>
</examples>
If the same transformation is applied on this XML document (the same as above, but with changed sortby and sort-order):
<examples>
<sortby>number</sortby>
<sort-data-type>number</sort-data-type>
<sort-order>ascending</sort-order>
<example>
<year>2008</year>
<number>3</number>
</example>
<example>
<year>2012</year>
<number>2</number>
</example>
<example>
<year>2010</year>
<number>5</number>
</example>
</examples>
then again the wanted, correct result is produced:
<examples>
<example>
<year>2012</year>
<number>2</number>
</example>
<example>
<year>2008</year>
<number>3</number>
</example>
<example>
<year>2010</year>
<number>5</number>
</example>
</examples>
Explanation:
The select attribute of xsl:sort can contain any XPath expression, so we can specify an expression that selects any child of the elements to be sorted, such that its name is the result of evaluating another XPath expression.
In XSLT, typically, all attributes other than select allow *AVT*s (Attribute Value Templates) to be specified in their values.