Re: Transforming text into a variable name

Subject: Re: Transforming text into a variable name
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 02 Nov 1999 19:32:44 -0800
At 99/10/20 11:16 -0700, Scott Sanders wrote:
Is there a way in XSLT to transform a attribute text value to a variable
name.

No, but you can use an attribute's value to find a variable's declaration in the stylesheet, which may work for what you are looking for.


For Example:

<search title="Search for record" var="test">

In the XSL I want to transform 'test' to the equivalent of <xsl:value-of
select="$test"/>

A working example is below. If your variables are not result tree fragments, you'll have to do some awkward string manipulation since string values are specified inside of quotes.


I hope this helps.

......... Ken

T:\sanders>type sanders.xml
<?xml version="1.0"?>
<test>
<search title="Search for record" var="test1"/>
<search title="Search for another" var="test2"/>
</test>

T:\sanders>type sanders.xsl
<?xml version="1.0"?>
<!DOCTYPE xsl:stylesheet [<!ENTITY nl "&#xd;&#xa;">]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:variable name="test1">Value of first variable</xsl:variable>
<xsl:variable name="test2">Value of second variable</xsl:variable>

<xsl:template match="/">
  <result>
    <xsl:apply-templates/>
  </result>
</xsl:template>

<xsl:template match="search">
  <!--go to the stylesheet file and find the top-level
      variable declaration whose name attribute is equal
      to the current node's var attribute-->
  <xsl:value-of select="@title"/>: '<xsl:value-of
     select="document('')/*/xsl:variable[@name=current()/@var]"/>'
</xsl:template>

</xsl:stylesheet>

T:\sanders>xt sanders.xml sanders.xsl
<result>
Search for record: 'Value of first variable'

Search for another: 'Value of second variable'

</result>
T:\sanders>


-- G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (Fax:-0995) Web site: XSL/XML/DSSSL/SGML services, training, libraries, products. Practical Transformation Using XSLT and XPath ISBN 1-894049-02-0 Next instructor-led training: 1999-11-08, 1999-11-09, 1999-12-05/06, - 1999-12-07, 2000-02-27/28, 2000-05-11/12, 2000-05-15


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread