The XSLT style sheet allows you to operate the data in the original XML document very free. However, sometimes, when you really want to make some actual programming, the XSLT Recommendation is designed to do so by using an extension. These extensions use functions and elements to write any language supported by the processor. One of your choices is to embed the style sheet directly or as an external file.
This skill article uses Apache Project's Xalan Java 2 conversion engine and its implementation (see Resources). The overall concept is the same for any implementation, and the XSLT Recommendation does not require any special implementation methods. In addition to Xalan, in your ClassPath contains a js.jar file (see Reference), it contains JavaScript implementation, but also a bsf.jar file, which is part of the Xalan release.
The Source Document Style Table Documentation Documentation describes the items in the guess game, where the guess is guess from 1 to 100. Style sheets get these three numbers and compare them with random numbers. The sample document contains two groups of guess numbers:
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" thisguess "." } 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.) The component itself specifies all code from the Result namespace prefix to call it inside. 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 "Contest" 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.