[XML4Lib] XSLT call name of variable dynamically?

Luckabaugh, Joy H. (LNG-BET) Joy.Luckabaugh at lexisnexis.com
Thu Feb 5 16:42:22 EST 2009


You can pass element *content* into variable names when creating
self-generating xslt code, though:

Say you have a data structure:

parent/child/grandchild/element/childofelement

and you want the output element name to be coded as the text node of the
childofelement element.

<xsl:template match="parent" >
		<xsl:choose>
			<xsl:when test="whatever">
				<gen:template
match="{child/grandchild/element}">
					<gen:element
name="{childofelement}">whatever</gen:element>
				</gen:template>
			</xsl:when>

etc.

(note: gen is the namespace that the generated xslt code will be in;
this separates it from the )

if the whatever test is met, an XLST template is automatically generated
that matches the "element" element and THAT TEMPLATE can use xml element
content to make an element name.

Whether this is suited to your project, I can't tell from your
description.  I've used it.  I do know I spent about 3 days trying to do
what you are trying to do with putting variables into element names and
gave up because AFAICT it can't be done, and going with the page-long
xsl:choose extravaganza because it worked.

Joy

						

> -----Original Message-----
> From: Walker, David [mailto:dwalker at calstate.edu] 
> Sent: Thursday, February 05, 2009 4:27 PM
> To: Luckabaugh, Joy H. (LNG-BET)
> Cc: xml4lib at webjunction.org
> Subject: RE: [XML4Lib] XSLT call name of variable dynamically?
> 
> You mean like an XML file that holds the name of the text?  
> Yeah, I've considered this approach too. 
> 
> I prefer having it in XSLT variables, since we've done the 
> other labels like that.  And it allows a site not simply to 
> define text, but can to put XSLT code in the variable.  Which 
> is handy sometimes.
> 
> --Dave
> 
> ==================
> David Walker
> Library Web Services Manager
> California State University
> http://xerxes.calstate.edu
> ________________________________________
> From: Luckabaugh, Joy H. (LNG-BET) [Joy.Luckabaugh at lexisnexis.com]
> Sent: Thursday, February 05, 2009 1:23 PM
> To: Walker, David
> Cc: xml4lib at webjunction.org
> Subject: RE: [XML4Lib] XSLT call name of variable dynamically?
> 
> Please ignore this if there is some reason this wouldn't 
> work, but couldn't you do something similar by using external 
> entities (in local
> files) to hold the text?
> 
> Joy
> 
> > -----Original Message-----
> > From: xml4lib-bounces at webjunction.org
> > [mailto:xml4lib-bounces at webjunction.org] On Behalf Of Walker, David
> > Sent: Thursday, February 05, 2009 3:58 PM
> > To: Alexander Johannesen
> > Cc: xml4lib at webjunction.org
> > Subject: RE: [XML4Lib] XSLT call name of variable dynamically?
> >
> > Hopefully this makes sense:
> >
> > I've got this app that basically does a bunch of stuff, 
> creates some 
> > XML, and then uses XSLT to build the end-user interface in HTML.
> >
> > We've been using global <xsl:variable>s to hold text in the 
> system.  
> > Each implementation has a local stylesheet that imports the master 
> > one, so you can override the default system text by simply 
> > re-declaring the variable in the local stylesheet.  Works well.
> >
> > What I'm working on now is breadcrumbs in the system.  Right now we 
> > just have a big, hideous <xsl:choose> block that figures out which 
> > 'page' you are on, and then outputs some HTML for breadcrumbs.  
> > Doesn't work so well.
> >
> > What I'd _like_ to do is have the system itself create the 
> breadcrumb 
> > hierarchy in the XML, and then simply loop over that in the XSLT, 
> > outputting the link and text.  But I'd like the text itself to come 
> > from an XSLT variable, not from the XML, so we can keep our 
> override 
> > scheme.
> >
> > I can have the XML tell the XSLT which variable to use, but 
> I'm back 
> > to a (slightly less) hideous choose block:
> >
> > <xsl:choose>
> >   <xsl:when test="label = 'text_breadcrumb_databases_categories'">
> >     <xsl:copy-of select="$text_breadcrumb_databases_categories" />
> >   </xsl:when>
> >   [etc.,etc.]
> > </xsl:choose>
> >
> > The other option I've tried is to create templates that 
> match on the 
> > name of the XML node, so like this:
> >
> >   <xsl:template
> > match="text_breadcrumb_databases_categories">some 
> text</xsl:template>
> >
> > . . . and then in the breadcrumb section, something like this
> >
> >   <xsl:for-each select="breadcrumb">
> >      <a href="{link}"><xsl:apply-templates select="label" /></a>
> >   </xsl:for-each>
> >
> > But I've had some problems with this approach.  Need to 
> figure it out, 
> > unless there is some better way I'm not thinking of?
> >
> > --Dave
> >
> > ==================
> > David Walker
> > Library Web Services Manager
> > California State University
> > http://xerxes.calstate.edu
> > ________________________________________
> > From: Alexander Johannesen [alexander.johannesen at gmail.com]
> > Sent: Thursday, February 05, 2009 12:10 PM
> > To: Walker, David
> > Cc: xml4lib at webjunction.org
> > Subject: Re: [XML4Lib] XSLT call name of variable dynamically?
> >
> > On Thu, Feb 5, 2009 at 21:00, Walker, David <dwalker at calstate.edu> 
> > wrote:
> > >   <xsl:value-of select="${$foo}" />
> >
> > Nope, sorry, all variables and references to variables are 
> static by 
> > design (functional programming vs. imperative programming). 
> What are 
> > you trying to do, though?
> >
> >
> > Regards,
> >
> > Alex
> > --
> > --------------------------------------------------------------
> > -------------
> >  Project Wrangler, SOA, Information Alchemist, UX, 
> RESTafarian, Topic 
> > Maps
> > ------------------------------------------
> > http://shelter.nu/blog/ --------
> >
> > _______________________________________________
> > XML4Lib mailing list
> > XML4Lib at webjunction.org
> > http://lists.webjunction.org/mailman/listinfo/xml4lib
> >
> >
> 




More information about the XML4Lib mailing list