Oracle8i Application Developer's Guide - XML Release 3 (8.1.7) Part Number A86030-01 |
Library | Service | Contents | Index

Using XML Parser for Java, 12 of 22

## XML Extension Functions for XSL-T Processing

### XSL-T Processor Extension Functions: Introduction

XML extension functions for XSL-T processing allow users of XSL-T processor to call any Java method from XSL expressions.

Java extension functions should belong to the namespace that starts with the following:

    http://www.oracle.com/XSL/Transform/java/

An extension function that belongs to the following namespace:

    http://www.oracle.com/XSL/Transform/java/classname

refers to methods in class `classname`.

For example, the following namespace:

    http://www.oracle.com/XSL/Transform/java/java.lang.String

can be used to call`java.lang.String` methods from XSL expressions.

### Static Versus Non-static Methods

If the method is a non-static method of the class, then the first parameter will be used as the instance on which the method is invoked, and the rest of the parameters are passed on to the method.

If the extension function is a static method, then all the parameters of the extension function are passed on as parameters to the static function.

#### XML Parser for Java - XSL Example 1: Static function

The following XSL, static function example:

    <xsl:stylesheet
    xmlns:math="http://www.oracle.com/XSL/Transform/java/java.lang.Math">
      <xsl:template match="/">
      <xsl:value-of select="math:ceil('12.34')"/>
    </xsl:template>
    </xsl:stylesheet>

prints out '13'.

### Constructor Extension Function

The extension function 'new' creates a new instance of the class and acts as the constructor.

#### XML Parser for Java - XSL Example 2: Constructor Extension Function

The following constructor function example:

    <xsl:stylesheet
    xmlns:jstring="http://www.oracle.com/XSL/Transform/java/java.lang.String">
      <xsl:template match="/">
      <!-- creates a new java.lang.String and stores it in the variable str1 -->
      <xsl:variable name="str1" select="jstring:new('Hello World')"/>
      <xsl:value-of select="jstring:toUpperCase($str1)"/>
    </xsl:template>
    </xsl:stylesheet>

prints out 'HELLO WORLD'.

### Return Value Extension Function

The result of an extension function can be of any type, including the five types defined in XSL:

- NodeList

- boolean

- String

- Number

- resulttree

They can be stored in variables or passed onto other extension functions.

If the result is of one of the five types defined in XSL, then the result can be returned as the result of an XSL expression.

### XML Parser for Java XSL Example 3: Return Value Extension Function

Here is an XSL example illustrating the Return value extension function:

    <!-- Declare extension function namespace -->
    <xsl:stylesheet xmlns:parser =
    "http://www.oracle.com/XSL/Transform/java/oracle.xml.parser.v2.DOMParser"
    xmlns:document =
    "http://www.oracle.com/XSL/Transform/java/oracle.xml.parser.v2.XMLDocument" >

    <xsl:template match ="/"> <!-- Create a new instance of the parser, store it in
    myparser variable -->
    <xsl:variable name="myparser" select="parser:new()"/>
    <!-- Call a non-static method of DOMParser. Since the method is anon-static
    method, the first parameter is the instance on which themethod is called. This
    is equivalent to $myparser.parse('test.xml') -->
    <xsl:value-of select="parser:parse($myparser, 'test.xml')"/>
    <!-- Get the document node of the XML Dom tree -->
    <xsl:variable name="mydocument" select="parser:getDocument($myparser)"/>
    <!-- Invoke getelementsbytagname on mydocument -->
    <xsl:for-each select="document:getElementsByTagName($mydocument,'elementname')">
    ......
    </xsl:for-each> </xsl:template>
    </xsl:stylesheet>

### Datatypes Extension Function

Overloading based on number of parameters and type is supported. Implicit type conversion is done between the five XSL types as defined in XSL.

Type conversion is done implicitly between (String, Number, Boolean, ResultTree) and from NodeSet to (String, Number, Boolean, ResultTree).

Overloading based on two types which can be implicitly converted to each other is not permitted.

### XML Parser for Java Example 4: Datatype Extension Function

The following overloading will result in an error in XSL, since String and Number can be implicitly converted to each other:

- abc(int i){}

- abc(String s){}

Mapping between XSL type and Java type is done as following:

    String -> java.lang.String
    Number -> int, float, double
    Boolean -> boolean
    NodeSet -> XMLNodeList
    ResultTree -> XMLDocumentFragment

| Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
Library | Service | Contents | Index