0

I have created a file "name.js" also I have "name.xslt" I want to call function of getName from "name.js" in "name.xslt". For that, I need to import "name.js" and then call the function from that file.

Please help me how to import "name.js" in XSLT and how to call the function from the js file?

3 Answers 3

1

Unless you use an XSLT 2.0 processor supporting the use of Javascript to implement extension functions this is not possible. In a comment you mention you use Saxon, Saxon does not support this as it first of all allows you to implement your own functions with pure XSLT and XPath using xsl:function https://www.w3.org/TR/xslt-30/#stylesheet-functions and beyond that allows you to write extension functions in Java (for its Java version) or in C# or VB.NET for its .NET version, see http://saxonica.com/html/documentation/extensibility/ for details.

Some other XSLT like Altova or Xml Prime might give you some support to use Javascript or JScript to implement extension functions but in general that is limited to the core engines of these languages so depending of what your script file does it might not help at all, assuming that the script file tries to use browser API specific objects and functions like e.g. window, document.

Sign up to request clarification or add additional context in comments.

Comments

0

You will need two xslt file first will contain javascript code and second will import it for example: first file name is first.xslt(here is javascript):

<msxsl:script language="JScript" implements-prefix="user">
    function getName(nodelist) {
      return "David";
    }
</msxsl:script>

Second file will be main.xslt(here you will import first.xslt):

 <xsl:import href="first.xsl"/>

 <xsl:template match="/">
   <xsl:value-of select="user:getName(.)"/>
 </xsl:template>

4 Comments

I am using xslt2.0 saxon processor. so there may be another way to use js file. I have imported js file using this syntax <xsl:include href="/static/js/name.js"/> but not able to call function init.
I think it would work with html tag too. for example like this <script type="text/javascript" src="SomePath/SomeFileName.js">&#160;</script> try adding it and change the source path into your desired path
but how it will know which function to call, because in js file there are multiple functions.
I have never tried the second method which I have commented I found it on web just try calling the function by name right after yo will include script file. if it does not work first way will surely work
0

If you're using the Java-based Saxon processor and if it's really important to call this JS code then you could write a Java extension function that invokes the JS code using Nashorn, and call the Java extension function from Saxon. However, unless the JS code is really something very special, rewriting its logic in XSLT or in Java is probably going to be less effort.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.