Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Building Dynamic Urls!! [Thread Next] Re: Building Dynamic Urls!!To: NULL Date: 5/7/2005 2:19:00 PM
AJ wrote:
> I am wanting to build dynamic urls in xslt by using string concatenation,
> unless there is a better way. My stylesheet will be sent a $url parameter to
> provide the base of the url. From their my logic is as follows:
>
> <xsl:if test=â€child::areaâ€>
> Building_url = $url and “?area=†and <xsl:value-of select=â€areaâ€/>
> </xsl:if>
>
> <xsl:if test=â€child::actionâ€>
> Building_url = $url and “&action=†and <xsl:value-of select=â€actionâ€/>
> </xsl:if>
>
> <xsl:if test=â€child::pageâ€>
> Building_url = $url and “&page=†and <xsl:value-of select=â€pageâ€/>
> </xsl:if>
>
> I as I understand it, xsl variables can’t be changed once given a value.
> So what would be the best way to go about building these dynamic urls.
The content of an xsl:variable can be build using template calls,
conditional statements, here is an example:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:param name="URL"
select="'http://example.com/2005/05/07/whatever.asp'" />
<xsl:template match="/">
<results>
<xsl:apply-templates />
</results>
</xsl:template>
<xsl:template match="url-data">
<xsl:variable name="resultURL">
<xsl:value-of select="$URL" />
<xsl:text>?</xsl:text>
<xsl:call-template name="make-name-value-pair">
<xsl:with-param name="arg" select="area" />
</xsl:call-template>
<xsl:call-template name="make-name-value-pair">
<xsl:with-param name="arg" select="action" />
</xsl:call-template>
<xsl:call-template name="make-name-value-pair">
<xsl:with-param name="arg" select="page" />
</xsl:call-template>
</xsl:variable>
<url><xsl:value-of select="$resultURL" /></url>
</xsl:template>
<xsl:template name="make-name-value-pair">
<xsl:param name="arg" />
<xsl:if test="$arg">
<xsl:value-of select="name($arg)" />
<xsl:text>=</xsl:text>
<xsl:value-of select="$arg" />
<xsl:text>&</xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
With some example XML input being
<?xml version="1.0" encoding="UTF-8"?>
<url-data>
<area>Washington</area>
<action>delete</action>
<page>1</page>
</url-data>
the result of the transformation is
<?xml
version="1.0"?><results><url>http://example.com/2005/05/07/whatever.asp?area=Washington&action=delete&page=1&</url></results>
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
