wensveen
wensveen

Reputation: 884

Sitecore link url encoding (XSLT)

I'm generating links to my items in xslt using the following code:

<sc:link select=".">
  <xsl:value-of select="sc:fld('SomeField',.)" />
</sc:link>

Unfortunately the generated URLs contain spaces when the item names have spaces in them, instead of %20. I'm hoping there is some setting I can tweak to fix this. Does anybody know a solution?

Upvotes: 0

Views: 713

Answers (3)

wensveen
wensveen

Reputation: 884

Answering my own question because I like my solution slightly better than Mark's.

I've added a public static method public static string UrlPathEncode(string) to a class that is added as an xsl extension class:

public static string UrlPathEncode(string url)
{
    return HttpUtility.UrlPathEncode(url);
}

Add this to web.config:

<xslExtensions>
  <extension mode="on" type="MyProject.Xsl.XslHelper, MyProject"
    namespace="http://www.myproject.net/sc" singleInstance="true" />
  ..

Add a reference to the xslt file:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:mp="http://www.myproject.net/sc"
  ..

Now you can use the UrlPathEncode function like this:

<a href="{mp:UrlPathEncode(sc:path(.))}">
  <xsl:value-of select="sc:fld('SomeField',.)" />
</a>

Not really an elegant solution, but it does encode every character that is disallowed in URL paths.

Upvotes: 0

Helvdan
Helvdan

Reputation: 436

Simply use "translate" function:

<sc:link select=".">
    <xsl:value-of select="sc:fld('SomeField',translate(., ' ', '-'))"/>
</sc:link>

Upvotes: 0

Mark Cassidy
Mark Cassidy

Reputation: 5860

One solution is to use Sitecores encodeNameReplacers.

Find that section in web.config and add:

<replace mode="on" find=" " replaceWith="-" /> 

More info here: http://sitecoreninja.blogspot.co.uk/2013/03/replace-space-with-dash-in-url.html

Upvotes: 4

Related Questions