Forum Moderators: coopster & phranque

Message Too Old, No Replies

URL escaping in XSLT

Can you do it short of writing the worlds biggest replace function?

         

joshie76

11:39 am on Apr 11, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm using QueryStrings (?id=) in an app and the pages are drawn from XSLT but I need to escape the data passed in the querystring.

Does anybody know how to do this? I understand SAXON does it automatically with href attributes but we're using MSXML4. Any ideas?

Xoc

2:31 pm on Apr 11, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm having a hard time picturing what it is that you want to do. Can you give some code showing where you are having the problem? Is the querystring already in the XSL file, or is it passed in from a parameter or calculated in some way?

joshie76

7:05 pm on Apr 11, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK. I have a string in a node...

<node>{908-87238912-89732}</node>

And I want to make this node part of a querystring, eg:

<a> 
<xsl:attribute name="href">
<xsl:text>test.asp?id=</xsl:text>
<xsl:value-of select="node"/>
</xsl:attribute>
link
</a>

but this would result in
<a href="test.asp?id={908-87238912-89732}">link</a>

but what I really want is
<a href="test.asp?id=%7B908%2D87238912%2D89732%7D">link</a>

I can't find a function that can do this in XSLT, in ASP you'd use Server.URLencode(string) and in Client JScript escape(string). I read (never used it) that SAXON does this automatically - I think it must escape the whole href attribute though.

I could of course have some javascript do it all on the client though I'd like to avoid this.

Xoc

4:02 pm on Apr 12, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, got it.

The easiest way is to encode in in the XML. Thus:

<node>%7B908%2D87238912%2D89732%7D</node>

But if that isn't possible, the MSXML parser has a way of running JavaScript as it parses the file. I can't look it up right now, but if you can't find in the MSXML parser docs, send me a sticky and I'll try to help you out.

joshie76

4:15 pm on Apr 12, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



XOC, you're spot on... thanks for pointing me in the right direction - works a treat! Here's a snippet of the code I'm now using.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="http://mydomain.com/myname">

<msxsl:script language="JScript" implements-prefix="user">
function uriencode(string) {
return escape(string);
}
</msxsl:script>

<xsl:template match="/">
<xsl:variable name="test" select="'h kjs lskdf'"/>
<xsl:value-of select="user:uriencode($test)"/>
</xsl:template>

</xsl:stylesheet>

joshie76

6:28 pm on Apr 17, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I put my code in to practice and it fell over straight away. It seems if your passing it node (text) values you need to convert it to a string first otherwise you get JScript runtime errors - ugly ones too.

eg:

<xsl:value-of select="user:uriencode(string(xpath/xpath/xpath))"/>

OR

<xsl:variable name="myvariable" select="string(xpath/xpath/xpath)"/>

<xsl:value-of select="user:uriencode($myvariable)"/>

<added>Just though - I didn't try converting to a string inside the function, I'll try that tomorrow;)</added>