Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Building Dynamic Urls!!

From: Martin Honnen <mahotrash@-----.-->
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>&amp;</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&amp;action=delete&amp;page=1&amp;</url></results>


-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent