Search code examples
xsltjscriptmsxsl

How to pass node value to custom msxsl:script function?


I have a custom function in my XSLT document like so:

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

When I try passing it the value of a node like this:

<xsl:variable name="urlp" select="Path/To/Some[@attr='condition']/Node" />
<xsl:value-of select="custom:uriencode($urlp)" />

It gives me this result:

MS.Internal.Xml.XPath.XPathSelectionIterator

So I gather that I haven't passed the actual value of the Node to my function, but how do I do that? Or alternatively, retrieve the value of the node from the object that's being passed to my function?


Solution

  • If you want to pass in an XSLT/XPath string which is treated in the "script" function as a .NET String then use <xsl:value-of select="custom:uriencode(string($urlp))" />.

    If you want to solve it on the .NET side then call MoveNext() e.g.

    function encode(nodeIterator) {
      if (nodeIterator.MoveNext()) {
        return encodeURIComponent(nodeIterator.Current.Value);
      }
      else {
        return '';
      }
    }