Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Replacing a string segment inside a variable?

From: Martin Honnen <mahotrash@-----.-->
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('&amp;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&amp;Source=http://localhost/whatever.aspx">Click 
This</a></li>

    <li><a 
href="http://localhost/foo/bar/EditForm.aspx?ID=7&amp;Source=http://localhost/whatever.aspx">Click 
This</a></li>

    <li><a 
href="http://localhost/foo/bar/whatever.aspx&amp;Source=http://localhost/whatever.aspx">Click 
This</a></li>

</ul>


-- 

	Martin Honnen --- MVP XML
	http://msmvps.com/blogs/martin_honnen/


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