-1

I have the following XML node:

<CARRIERNAME>
    <![CDATA[AT&T]]>
</CARRIERNAME>

I need to place a JavaScript function to be applied on the output which is in this case:"AT&T" to format it.

Here is my approach:

<xsl:value-of select="user:Format_escapeEmbeddedCommas(string(CARRIERNAME))"/>

And also I have :

<msxsl:script language="JScript" implements-prefix="user">
    function Format_escapeEmbeddedCommas(colValue) {
        var strValue = colValue;
        strValue = strValue.replace('&amp;','&');
        return strValue;
    }
</msxsl:script>

I tried to run it on http://xslt.online-toolz.com/tools/xslt-transformation.php, but it is returning the following error:

Error:XSLTProcessor::transformToXml() [xsltprocessor.transformtoxml]: XPath evaluation returned no result.

To see the whole script,please check this Fiddle. I would appreciate your help.

Scott
  • 1,863
  • 2
  • 24
  • 43
Abadi
  • 71
  • 2
  • 9
  • Well as you tagged php; why not use it –  Sep 03 '15 at 08:26
  • I refered to this answer on stackoverflow,and it is also producing the same error:http://stackoverflow.com/questions/7444317/how-to-include-javascript-file-in-xslt – Abadi Sep 03 '15 at 08:52
  • possible duplicate of [How to use javascript functions in xsl document?](http://stackoverflow.com/questions/32372948/how-to-use-javascript-functions-in-xsl-document) – Abel Sep 03 '15 at 11:39

1 Answers1

0

Short answer: it looks like you use XSLT with PHP (at least you tagged it PHP), but you are using JScript in the extension function, which is Microsoft, and you use msxsl:script, which is also a Microsoft extension instruction.

Your extension function presently is illegal in XML, so you cannot possibly run the code above (you have an unescaped &), so it should not run on any processor.

If you use PHP you are likely using libxslt, here's a tutorial. Not sure if you can use extension functions, but from the description I doubt you actually need any, as replacing characters in a value can be done without extension functions (use translate).

strValue = strValue.replace('&amp;','&');

This is an error, as mentioned above. But it looks like you try to prevent your stylesheet to output &amp;, which means you are trying to prevent your stylesheet from creating legal XML. This is a slippery slope, but if you really need it, simply use xsl:value-of with disable-output-escaping. The effect will only be visible if you actually serialize the output, though, and if the processor supports it (it is not required to do so).

Here's a question on exactly the same subject where I showed you how to resolve this the easy way. Perhaps you should try to first try the simple solution (just using XSLT) and only if that does not suffice, resort to a more complex solution (using extension functions)?

I have the impression that you are just starting out with XSLT and have a lot of questions. That is good. But it seems that these questions are related to understanding the basics of XML, which precludes understanding XSLT. There are plenty tutorials on XML and XSLT, give yourself some time to read up on those, as otherwise it may continue to be challenging and confusing.

Community
  • 1
  • 1
Abel
  • 56,041
  • 24
  • 146
  • 247