Tip: Call JavaScript from An XSLT style sheet

Source: Internet
Author: User
Tags xslt
XSLT
Style sheets allow you to operate on data in the original XML document very freely. However, sometimes, when you really want to perform some practical programming, XSLT
The proposal is designed to allow this by using extensions. These extensions are in the form of functions and elements that can be written in any language supported by the processor. One of your options is
Embed a style sheet directly or as an external file.

This tipArticleUse the xalan Java 2 conversion engine of Apache project and its implementation (see references ). The overall concept is the same for any implementation, and the XSLT proposal does not require any special implementation methods. Except xalanClasspathThe JS. jar file is also required (see references), which includes JavaScript implementation andBSF. JarFile, which is part of the xalan release.

Source Document
The style sheet document example records the items in the game, where the number of guesses starts from 1 to 100. The style sheet obtains the three numbers and compares them with the random number. The sample document contains two sets of guesses:

Sample document

<? XML version = "1.0"?>
<Entries gameid = "dwo">
<Entry>
<Player> JOHN </player>
<Guess> 3 </guess>
<Guess> 9 </guess>
<Guess> 222 </guess>
</Entry>
<Entry>
<Player> Mary </player>
<Guess> 88 </guess>
<Guess> 76 </guess>
<Guess> 5 </guess>
</Entry>
</Entries>

Create component
The first step to using extended elements or functions is to defineCode. This involves defining a new namespace and container for the Code:

Basic Style Sheets

  
       



function getresult (thisguess)
{< br> var thisresult = parseint (math. ran Dom () * 100);
If (thisresult = parseint (thisguess)
{< br> return "correct! ";
}< br> else
{< br> return" wrong! The actual answer was "+ thisresult +", not "+ thisguess + ". ";
}< BR >}< br>




On the surface, this is a typical style table with two new namespaces added. The prefix of the first namespace isLxsltTo tell the processor which element defines the new function. The prefix of the second namespace isResultIndicates a call to the new function. Finally,Extension-element-prefixesAttribute to let the processor know which element should not be converted as part of a normal stream. (As we will see, they will still return a value as output .)

The component itself specifiesResultThe namespace prefix calls all internal code. It also allows the processor to know which functions will be called from the extension element and which functions will be called from the extension function. The script element describes the function itself.

In this example, we start with a function, which gets a parameter and compares it with a random number between 1 and 100, and returns a string representing the result.

Extended Functions
In the XSLT style sheet, extended functions actually expand XPath. Therefore, you can use built-in functions suchTranslate ()OrRound ().

Call a function

...
<XSL: template match = "/">
<XSL: Apply-templates/>
</XSL: Template>
<XSL: template match = "entry">
Guesser:
<XSL: value-of select = "Player"/>
<XSL: Apply-templates select = "Guess"/>
</XSL: Template>
<XSL: template match = "Guess">
Guess:
<XSL: value-of select = "."/>
Actual:
<XSL: value-of select = "Result: getresult (string (.)"/>
</XSL: Template>
</XSL: stylesheet>

In this example (Guess).Getresult ()Function. The namespace allows the processor to know the functions in the trigger result component.

Figure 1. Initial Results

Use Element
Extension elements are a little more complex than functions. We do not want the extension elements to simply return a value (although they can do this), but want them to perform a specific operation at a specific "time" during the style sheet processing. You do not want to obtain a random parameter list (because the extension function can also be used). The code behind the extension element contains two well-defined parameters.

RulesElement triggerRules ()Function processing. This function willRulesElement itself (ELEMAs one of its parameters, you can retrieve the value of any custom attribute that it has.

Use processor context
The most powerful aspect of the extension element is the ability to access the source document itself through the context parameters of the XSL processor.

Processor context

...
<Lxslt: component prefix = "result" elements = "rules" functions = "getresult">
<Lxslt: script lang = "JavaScript">
...
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>
...

RulesThe first parameter of the function isOrg. Apache. xalan. Extensions. extends processorcontextThe processing context in the object form. This allows you to retrieve the conversion that represents the context node, the entire source tree, the style sheet, and the current conversion.Program. Access to context nodes is the most common. OnceGetcontextnode ()Method return, which is a typical XML node that can use typical Dom operations.

Figure 2. Final output

References

    • See the W3C XSLT proposal.
    • Download Apache xalan-Java 2.
    • Download the Js. jar file.
    • Find more XML references in the developerworks XML area.
    • IBM WebSphere Studio Application Developer is an easy-to-use integrated development environment for building, testing, and deploying Java Server Pages, Servlets, and XML-related applications and websites.
About the author
Nicolas
Chase was involved in Lucent Technologies, Sun Microsystems, Oracle, And Tampa Bay
Website development for multiple companies, including 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
Oracle instructor. Recently, he became site dynamics interactive communications in Clearwater, Florida.
The chief technology officer. He wrote three books about web development, includingJava and XML from scratch(Que) and to be publishedPrimer plus XML Programming(SAMs ). He is willing to listen to the reader's opinion and can contact him through the nicholas@nicholaschase.com.

From: http://www.cnblogs.com/sunsonbaby/archive/2005/01/17/93194.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.