URL and the & ampersand
Solution 1
There's nothing built in to do this, but rather than reinvent the wheel there are style sheets already out there e.g.:
http://skew.org/xml/stylesheets/url-encode/
The transforms are straightforward but (hopefully) someone else will have done the debugging for you...
Solution 2
In XML (and therefore XSL) you need to escape the &, > and < using
&
<
>
Solution 3
The fragment <description>one word & another</description>
resembles XML, but the naked ampersand is illegal. XSL requires legal XML.
One way to make it legal without substituting &
is to make the text into a CDATA section:
<description><![CDATA[one word & another]]></description>
Solution 4
Is it possible for you to use XSLT 2.0? I only mention it because there is a nifty URL encoding function which would sort out all your problems..
<xsl:value-of select="url:encode('This is URL encoded')" />
Solution 5
For my purposes, none of these solutions worked. I was working within a proprietary framework, and using the entity:
&
Was the only way I was able to append additional parameters to a URI string, in the context of an XSL template.
Matt W
I write code for fun and profit. Sometimes I blog about it. Not enough time for either. At work I break websites. At home I break physics based mobile games. "No code more than 8 lines ever worked as intended first time." - My Dad.
Updated on June 04, 2022Comments
-
Matt W over 1 year
Using XSLT and XPath 1.0, I have a string I want to escape for use on a URL, for example:
<description>one word & another</description>
So, the text() of the description element should get URL escaped.
How would I do this?
Using C# (XslCompiledTransform) the code would be:
string a = Server.UrlEncode("one word & another");
And would produce:
one+word+%26+another
Any suggestions?
-
Tim about 14 years+1, although you generally do not need to escape
>
. w3.org/TR/2008/REC-xml-20081126/#syntax -
Matt W about 14 yearsAbsolutely correct. I have adjusted my code snippet to compensate. Do you have an answer to my question, which is actually about how to replace the URL-illegal characters with valid ones ie to URL-escape the text?
-
Matt W about 14 yearsSo there's really no way to convert any short piece of text into a link, easily?
-
FinnNk about 14 yearsNot using XSLT alone - it's a generic transformation language, so it's not suprising really. You could start mixing in other technologies to simplify the task, for example you could manipulate the source XML (codeproject.com/KB/cpp/myXPath.aspx) by using Server.UrlEncode after picking out the nodes from an XmlDocument with xpath and then push the result through XSLT. Depends on the context really.
-
Matt W about 14 yearsThank you, those style sheets have helped enormously.
-
Matt W about 14 yearsI would like to use XSLT 2, but I'm not sure which implementation to use for .NET
-
Tomalak about 14 yearsTrue as this might be, this is watering down people's perception. It's simpler (and better, probably) to remember that
>
is not an exception to the rule. -
James Goodwin about 14 yearsMaybe this will point you in the right direction: stackoverflow.com/questions/94047/…