Browse DevX
Sign up for e-mail newsletters from DevX


XSLT Extension Functions in XDK 10g : Page 4

While XSLT alone is powerful, extending it with implementation-specific Java functions gives you all the power you could want.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Modifying Element Text

To modify the text in the title element, first retrieve the title node:

<xsl:variable name="titleNodeList" select="element:getElementsByTagName( $articleElement, 'title')"/> <xsl:variable name="titleNode" select="nodelist:item($titleNodeList, 0)"/> <xsl:variable name="titleTextNode" select="node:getFirstChild($titleNode)"/>

Then you can modify the title node's value directly:

<xsl:value-of select="node:setNodeValue( $titleTextNode, 'Oracle Certified Master')"/>

Modifying the author node follows the same steps.

Although XSLT is typically used for transforming an XML input into another format, sometimes that's not exactly what you need. In this article, you saw how to add power to XSLT by loading an XML file and altering its contents using Java class methods from within the stylesheet itself, using the XSLT extension functions to call the Java class methods from XSLT.

Too see the sample XSLT in action, copy the sample XSLT file parser.xslt shown in Listing 1 to a directory and run it with the command-line utility oraxsl. The syntax of the oraxsl command is:

>oraxsl source stylesheet

To convert the example XML document run the following oraxsl command that specifies parser.xslt as a source and as the stylesheet:

> oraxsl parser.xslt parser.xslt

Extend XSLT Functions

Normally, the first parameter to oraxsl specifies the file the stylesheet named in the second parameter will process; however, because this sample stylesheet doesn't use a source XML document, the first parameter is irrelevant. Still, it's required, and it has to be a valid file, so I simply used the stylesheet file again. In this case, the transformation will work identically regardless of which file you use for the first parameter.

To wrap up, you can use XSLT extension functions to extend the library of functions that XSLT provides. You can store the return values of XSLT extension functions in XSLT variables to use them in your stylesheets. Although—for demonstration purposes—this sample doesn't reflect the rule, you should normally use XSLT extension functions only if the XSLT doesn't provide the required functionality.

Deepak Vohra is an O'Reilly technical reviewer, who reviewed the book WebLogic: The Definitive Guide. He's also a NuBean consultant and web developer, and is a Sun Certified Java 1.4 Programmer and Sun Certified Web Component Developer for J2EE.
Thanks for your registration, follow us on our social networks to keep up-to-date