XSLT version 1 URL encoding

女生的网名这么多〃 提交于 2019-11-30 08:00:26

问题


Is there a URL encoding function in XSLT version 1? I need something similar to encodeURIComponent function in javascript?

Seeing as this is not possible as I'm using .NET platform as the XSLT is applied to a Sharepoint page. Is there a way to code this buy calling the javascript encode function within the XML, snippet below:

<xsl:template name="BuildLink">
    <xsl:param name="PrimarySubject" />
    <xsl:text>?PrimarySubject=</xsl:text>
    <xsl:value-of select="$PrimarySubject" />
</xsl:template>

Thanks,


回答1:


you can use JScript embedded in XSLT ...

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

and call it like custom:uriencode( url_to_encode )

You will first need to define the namespace though by adding to the <xsl:stylesheet ... tag the xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:custom="http://youdomain.ext/custom"

[update]

the Url i put for the custom namespace could be anything .. it is just a unique identifier..
(the link concludes that it is less confusing if you do not use a url as the identifier..)

[udpdate 2]

Some added info.
If you use MSXML6 then you need to manually allow scripting in the XSLT, by using the AllowXsltScript property.. ( reference )
Depending on the way you load the xslt and its settings have a look at the examples at Script Blocks Using msxsl:script on how to alow scripts




回答2:


These XSLT 1.0 stylesheets can be used to perform URL encode/decode:

  1. url-decode.xsl
  2. url-encode.xsl

Description: Only a very small subset of ASCII characters can be safely embedded in a URI. Characters outside this set must be escaped using so-called "URL encoding" or "%-escaping". The characters are converted to bytes according to some encoding (ASCII if possible) and those bytes are each written as a '%' followed by 2 hexadecimal digits. The encoding used as the basis for this conversion can be anything, but tends to be dictated by the context in which the URI is being used. Recent and future standards use UTF-8, but legacy applications including many widely-deployed, server-side processors of HTML form data assume ISO-8859-1.

For example, the greeting "¡Hola, César!" can be embedded in a URI if it is written as follows: %A1Hola%2C%20C%E9sar!. (...assuming ISO-8859-1 as the basis for encoding. If it were UTF-8 based, then it would be %C2%A1Hola%2C%20C%C3%A9sar!).

The url-encode.xsl demo URL-encodes an arbitrary string passed in as a parameter named iso-string. It assumes ISO-8859-1 will be the basis for the URL-encoding. It should be compatible with any XSLT 1.0 processor.

Decoding an ISO-8859-1 based URL-encoded string uses a similar technique. See url-decode.xsl for a demo. Its url parameter is the string to be decoded.




回答3:


There isn't one. XSLT has no knowledge of URLs and HTML.

Depending on the language and platform you are using, you may be able to use a XSLT extension that does that.




回答4:


You can, under certain circumstances use EXSLT (parser-provided extensions): Dave Pawson's XSLT FAQ on this one.

However, XSLT is Turing complete, so you could write a basic URL encoder in XSLT itself, theoretically (I wouldn't try).



来源:https://stackoverflow.com/questions/2425516/xslt-version-1-url-encoding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!