This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: Something like eval()? and Lookup Tables
- To: jeni at jenitennison dot com
- Subject: RE: [xsl] Something like eval()? and Lookup Tables
- From: "McKeever, Marty" <marty dot mckeever at bankofamerica dot com>
- Date: Wed, 07 Nov 2001 13:11:10 -0500
- Cc: xsl-list at lists dot mulberrytech dot com
- Reply-To: xsl-list at lists dot mulberrytech dot com
I wound up solving my own eval() problem, using the lookup table device.
Nifty indeed.
Heres what i came up with--
Input: <foo
hyperLink="some.htm?one={$one}&two={$two}&three={$three}">click
me</foo>
OutPut: <a href="some.htm?one=uno&two=dos&three=tres">click me</a>
Stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl">
<xsl:param name="one">uno</xsl:param>
<xsl:param name="two">dos</xsl:param>
<xsl:param name="three">tres</xsl:param>
<xsl:param name="four">quattro</xsl:param>
<xsl:variable name="lookupTable">
<table>
<item name="one"><xsl:value-of select="$one"/></item>
<item name="two"><xsl:value-of select="$two"/></item>
<item name="three"><xsl:value-of select="$three"/></item>
<item name="four"><xsl:value-of select="$four"/></item>
</table>
</xsl:variable>
<xsl:template match="foo">
<a>
<xsl:attribute name="href">
<xsl:call-template name="matchParams">
<xsl:with-param name="inputString" select="@hyperLink"/>
</xsl:call-template>
</xsl:attribute>
<xsl:value-of select="."/>
</a>
</xsl:template>
<xsl:template name="matchParams">
<xsl:param name="limit">100</xsl:param>
<xsl:param name="inputString"/>
<xsl:variable name="flagged"><xsl:if
test="contains($inputString,'{$')"><xsl:value-of
select="substring-after(substring-before($inputString,'}'),'{$')"/></xsl:if>
</xsl:variable>
<xsl:variable name="resultString">
<xsl:choose>
<xsl:when test="$flagged!=''">
<xsl:call-template name="replace">
<xsl:with-param name="str" select="$inputString"/>
<xsl:with-param name="from" select="concat('{$',$flagged,'}')"/>
<xsl:with-param name="to"
select="msxsl:node-set($lookupTable)/table/item[@name=$flagged]"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise><xsl:value-of select="$inputString"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="contains($resultString,'{$') and $limit > 0">
<xsl:call-template name="matchParams">
<xsl:with-param name="limit" select="$limit - 1"/>
<xsl:with-param name="inputString" select="$resultString"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise><xsl:value-of
select="$resultString"/></xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Search & Replace -->
<xsl:template name="replace">
<xsl:param name="str" select="."/>
<xsl:param name="from"/>
<xsl:param name="to"/>
<xsl:choose>
<xsl:when test="not($from) or not($to)"><xsl:value-of
select="$str"/></xsl:when>
<xsl:otherwise>
<xsl:variable name="result"><xsl:value-of
select="substring-after($str,$from)"/></xsl:variable>
<xsl:value-of
select="substring-before($str,$from)"/><xsl:value-of select="$to"/>
<xsl:choose>
<xsl:when test="contains($result,$from)">
<xsl:call-template name="replace">
<xsl:with-param name="str"><xsl:value-of
select="$result"/></xsl:with-param>
<xsl:with-param name="from"><xsl:value-of
select="$from"/></xsl:with-param>
<xsl:with-param name="to"><xsl:value-of
select="$to"/></xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise><xsl:value-of
select="$result"/></xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list