Turn JavaScript from XSLT style sheet (reproduced by author nicholas chase)
http://www-900.ibm.com/developerWorks/cn/xml/tips/x-tipxsltjs/index.shtml
Sample document
XML Version = "1.0"?>
entry>
entry>
entry>
The first step in creating a component using extended elements or functions is to define the code to be executed. This involves defining a new namespace and container for code:
Basic style sheet
XML Version = "1.0"?>
XMLns: lxslt = "http://xml.apache.org/xslt" XMLns: result = "http://www.example.com/results" EXTENSION-Element-Prefixes = "Result" Version = "1.0"> Function getResult (thisguess) { Var thisResult = parse (math.random () * 100); IF (thisResult == PARSEINT (THISGUESS)) { Return "Correct!"; } else { Return "Wrong! The Actual Answer Was" THISRESULT ", not" thisgues "." } } lxslt: Script> lxslt: Component> xsl: template> xsl: stylesheet> On the surface, this is a typical style sheet that adds two new namespaces. The first namespace prefix is LXSLT, telling which elements of the processor define new features. The second namespace prefix is Result, indicating a call to the new feature. Finally, the Extension-Element-Prefixes property allows the processor to know which element as part of the normal stream should not be converted. (Like we will see, they will still return a value as an output.) Component itself specifies all code from the Result namespace prefix to call all of its internal code. It also allows the processor to know which functions will call from the extended element, and which functions will be called from the extended function. Script elements description function itself. In this example, we start from a function, which acquires a parameter and compares it to the random number of 1 to 100, returns a string that represents the result. The extended function In the XSLT style sheet, the extension function actually expands XPath, so you can use them like use the built-in function (such as translate () or round ()). Call functions ... xsl: template> Guesser: xsl: template> Guess: Actual: xsl: template> xsl: stylesheet> This example passes a string value of the current node (Guess) to the getResult () function. The namespace allows the processor to know the functions in the trigger the result component. Figure 1. Preliminary results Use element expansion elements than the function more complicated. We don't want to expand elements to simply return a value (although they can do this), and they hope that their specific "time" in the style sheet process is performed. Never want to get a random parameter list (because the extension function can be), the code behind the extended element contains two good definitions. The Rules element triggers the process of the rules () function. This function will use the Rules element itself as one of its parameters, allowing you to retrieve the value of any custom properties it owns. Using the processor context extending element The most powerful aspect may be the ability to access the source document itself through the XSL processor context parameter. Processor context ... ... Function Rules (CTX, ELEM) { CTXNode = ctx.getContextNode (); GameID = ctxnode.getfirstchild (). getAttribute ("gameid"); return "conteT" GameID "is based on" ELEM.GETATTRIBUTE ("Guesstype") "Guesses." } lxslt: Script> lxslt: Component> ... The first parameter of the Rules function is the processor context of org.apache.xalan.extensions.xslprocessorContext object form. This allows you to retrieve objects representing the context node, the entire source tree, style sheet, and the converter currently execute. Access context nodes are the most common. Once returned by the getContextNode () method, this is a typical XML node that can be operated using a typical DOM. Figure 2. Final output Reference Please refer to the W3C XSLT Recommendation. Download Apache Xalan-Java 2. Download the js.jar file. Find more XML references on the developerWorks XML zone. IBM WebSphere Studio Application Developer is an easy-to-use integrated development environment for building, testing, and deploying Java Server Pages, servlets, and applications and websites related to XML. About the author Nicholas Chase has participated in the website development of many companies including Lucent Technologies, Sun Microsystems, Oracle and Tampa Bay Buccaneers. Nick was a high school physics teacher, a low-level radioactive waste facility administrator, an online science fiction magazine editor, a multimedia engineer and an Oracle lecturer. Recently, he became the Chief Technology Officer of Site Dynamics Interactive Communications in Clearwater, Florida. He wrote three books related to Web development, including Java and XML from Scratch (QUE), and upcoming PRIMER PLUS XML Programming (SAMS). He is happy to listen to the reader's opinion, you can contact him through Nicholas@nicholaschase.com.