[XML4Lib] XSLT call name of variable dynamically?

Alexander Johannesen alexander.johannesen at gmail.com
Fri Feb 6 03:00:49 EST 2009


Hi,

On Thu, Feb 5, 2009 at 23:49, Erik Hetzner <erik.hetzner at ucop.edu> wrote:
> <xsl:variable name="labels">
>  <labels>
>    <label name="text_breadcrumb_databases_categories" value="..."/>
>  </labels>
> </xsl:variable>

This requires XSLT 2.0 or an EXSLT extension to work (but I've seen
XSLT processors support this silently with XSLT 1.0 as well), but I'd
dump this out to an external XML document instead, and then use a
global to define the document ;

   <xsl:variable name="label" select="document('my_labels.xml')/*" />

and when you need it ;

   <xsl:value-of select="$label[@name='some_name_here']" />

Or, if you want to do it the even faster way, define a key-set over
the labels globally ;

   <xsl:key name="labels" match="$label" use="@name"/>

and use it locally ;

   <xsl:value-of select="key('labels, 'some_name_here')" />

Hope that makes more sense. In some of my own stuff I'd create a
function so that if you ask for a label of which the @name isn't
found, a default text (or, if it can't be found in one language but
exists in the default language, show that) comes up (or a message that
it doesn't exists). Again, depends on what you want.


regards,

Alex
-- 
---------------------------------------------------------------------------
 Project Wrangler, SOA, Information Alchemist, UX, RESTafarian, Topic Maps
------------------------------------------ http://shelter.nu/blog/ --------




More information about the XML4Lib mailing list