Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Re: Replacing a string segment inside a variable? [Thread Next] Re: Replacing a string segment inside a variable?To: NULL Date: 7/21/2009 7:25:00 PM
ChrisG wrote:
>> So you are probably better off to use a named template
>> that replaces a string and then construct the href attribute with
>> xsl:attribute where you use xsl:call-template inside of the xsl:attribute.
>
>
> Not sure I follow. Is it possible/easier to get the index's of the '/'
> and work backward?
You can use a named template that performs a string replacement:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" indent="yes"/>
<xsl:param name="PageUrl" select="'http://localhost/whatever.aspx'"/>
<xsl:template name="replace">
<xsl:param name="str"/>
<xsl:param name="substr"/>
<xsl:param name="rep"/>
<xsl:choose>
<xsl:when test="contains($str, $substr)">
<xsl:value-of select="concat(substring-before($str, $substr),
$rep)"/>
<xsl:call-template name="replace">
<xsl:with-param name="str" select="substring-after($str,
$substr)"/>
<xsl:with-param name="substr" select="$substr"/>
<xsl:with-param name="rep" select="$rep"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$str"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="links">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="link">
<li>
<a>
<xsl:attribute name="href">
<xsl:call-template name="replace">
<xsl:with-param name="str" select="@ows_URL"/>
<xsl:with-param name="substr" select="'/DispForm.aspx?'"/>
<xsl:with-param name="rep" select="'/EditForm.aspx?'"/>
</xsl:call-template>
<xsl:value-of select="concat('&Source=', $PageUrl)"/>
</xsl:attribute>
<xsl:text>Click This</xsl:text>
</a>
</li>
</xsl:template>
</xsl:stylesheet>
When run against
<links>
<link ows_URL="http://mySiteUrl/DispForm.aspx?ID=6"/>
<link ows_URL="http://localhost/foo/bar/DispForm.aspx?ID=7"/>
<link ows_URL="http://localhost/foo/bar/whatever.aspx"/>
</links>
the result is
<ul>
<li><a
href="http://mySiteUrl/EditForm.aspx?ID=6&Source=http://localhost/whatever.aspx">Click
This</a></li>
<li><a
href="http://localhost/foo/bar/EditForm.aspx?ID=7&Source=http://localhost/whatever.aspx">Click
This</a></li>
<li><a
href="http://localhost/foo/bar/whatever.aspx&Source=http://localhost/whatever.aspx">Click
This</a></li>
</ul>
--
Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
