Browse DevX
Sign up for e-mail newsletters from DevX


XSLT Extension Functions in XDK 10g : Page 3

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

Storing Java Results in XSLT

You can place values returned from non-static methods into XSLT variables.

For example, to obtain the parsed XMLDocument and save it into the variable named parsedDocument, you could write:

<xsl:variable name="parsedDocument" select="parser:getDocument($parser)"/>

You can use that to query the document further. To get the root node of the parsed XML document, for example, use:

<xsl:variable name="catalogElement" select="document:getDocumentElement($parsedDocument)"/>

You can obtain node lists as well as individual nodes. To modify the first journal node, first retrieve the list of journal nodes from the parsed XML document:

<xsl:variable name="journalNodeList" select="element:getElementsByTagName( $catalogElement,'journal')"/>

Then select the first node from the list:

<xsl:variable name="journalNode" select="nodelist:item($journalNodeList, 0)"/>

Because XSLT can't cast objects (remember?) you have to convert the journal node, which is of type org.w3c.dom.Node, to an org.w3c.dom.Element using the Java NodeToElement class's nodeToElement(Node node) method. You assign the result to an XSLT variable:

<xsl:variable name="journalElement" select="nodeElement:nodeToElement($journalNode)"/>

Note that the call was slightly different, because nodeToElement() is a static method. The syntax for a static method in an XSLT is...

select="<classprefix>:<classmethod>(<methodparam1>, <methodparam2>)"

...where <classprefix> is the namespace prefix for the Java class, <classmethod> is the static method you're calling, and <methodparam1>, <methodparam2> (etc.) are the static method parameters.

Modifying Attributes

To modify an element's attributes, you first remove the attribute, and then add a new attribute and value to the element. So, for example, to change the date attribute in the journal element, first remove it:

<xsl:value-of select="element:removeAttribute( $journalElement, 'date')"/>

Then create a new attribute and add it to the journal element:

<xsl:value-of select="element:setAttribute($journalElement, 'date', 'January-February 2004')"/>

The sample XSLT uses a similar process to modify the article element's section attribute.

Thanks for your registration, follow us on our social networks to keep up-to-date