0

I am using XSLT 1.0 and I am not supposed to use XSLT 2.0. I have the following xml in which the value of <prvNum> has some special characters.

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <prvNum>SPECIAL#1&amp;</prvNum>
</root>

Now I want to perform percent-encoding for the value of <prvNum>. For example the value should be changed as below after percent encoding:

SPECIAL%231%26

I am trying with the following code snippet, but the stylesheet is not compiling.

 <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:custom="http://youdomain.ext/custom" xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0">
   <msxsl:script language="JScript" implements-prefix="custom">function uriencode(string) {
 return encodeURIComponent(string);
}</msxsl:script>
   <!-- identity template -->
   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@* | node()" />
      </xsl:copy>
   </xsl:template>
   <xsl:template match="prvNum">
      <prvNum>
         <xsl:copy-of select="@*" />
         <xsl:value-of select="custom:uriencode(text())" />
      </prvNum>
   </xsl:template>
</xsl:stylesheet>

Can anybody help me to fix the issue?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Ashok.N
  • 1,327
  • 9
  • 29
  • 64
  • I suspect you need to bind the `custom:` prefix to a namespace before you can use it. – michael.hor257k Feb 10 '17 at 13:24
  • @michael.hor257k, Thanks for the response. I am getting the error `Unable to generate the XML document using the provided XML/XSL input. Cannot find a matching 1-argument function named {http://youdomain.ext/custom}uriencode()` after I bound the `custom` prefix. Could you please help me out in figuring out the solution? – Ashok.N Feb 10 '17 at 13:45
  • Are you in fact using a MSXSL processor? – michael.hor257k Feb 10 '17 at 13:57
  • @ michael.hor257k, Honestly I am not sure which processor I am using. We are using java and Spring Integration in our project. How to know which processor I am using? – Ashok.N Feb 10 '17 at 14:00
  • The code above will only work in Microsoft's MSXSL: https://msdn.microsoft.com/en-us/library/533texsx%28v=vs.110%29.aspx See here how to find which processor you have: http://stackoverflow.com/questions/25244370/how-can-i-check-which-xslt-processor-is-being-used-in-solr/25245033?s=1|0.1818#25245033 – michael.hor257k Feb 10 '17 at 14:05
  • @michael.hor257k, I am using "Apache Software Foundation (Xalan XSLTC)" processor. – Ashok.N Feb 10 '17 at 14:11
  • Then look at: https://xalan.apache.org/old/xalan-j/extensions_xsltc.html – michael.hor257k Feb 10 '17 at 14:13

0 Answers0