<?xml version="1.0" encoding="iso-8859-1"?> <?xml-stylesheet href="../../make-menu.xsl" type="text/xsl"?><html> <head> <this-is section="extensibility" page="functions" subpage="staticmethods"/> <!-- Generated at 2011-12-09T20:47:22.916Z--><title>Saxonica: XSLT and XQuery Processing: Calling Static Methods in a Java Class</title> <meta name="coverage" content="Worldwide"/> <meta name="copyright" content="Copyright Saxonica Ltd"/> <meta name="title" content="Saxonica: XSLT and XQuery Processing: Calling Static Methods in a Java Class"/> <meta name="robots" content="noindex,nofollow"/> <link rel="stylesheet" href="../../saxondocs.css" type="text/css"/> </head> <body class="main"> <h1>Calling Static Methods in a Java Class</h1> <p><b>Static methods</b> can be called directly.</p> <p>For example (in XSLT):</p> <div class="codeblock" style="border: solid thin; background-color: #B1CCC7; padding: 2px"> <pre> <code><xsl:value-of select="math:sqrt($arg)" xmlns:math="java:java.lang.Math"/> </code> </pre> </div> <p>This will invoke the static method <code>java.lang.Math#sqrt()</code>, applying it to the value of the variable <code>$arg</code>, and copying the value of the square root of <code>$arg</code> to the result tree.</p> <p>Similarly (in XQuery):</p> <div class="codeblock" style="border: solid thin; background-color: #B1CCC7; padding: 2px"> <pre> <code><a xmlns:double="java:java.lang.Double"> {double:MAX_VALUE()} </a> </code> </pre> </div> <p>This will output the value of the static field <code>java.lang.Double#MAX_VALUE</code>. (In practice, it is better to declare the namespace in the query prolog, because it will then not be copied to the result tree.)</p> <p>A static Java method called as an extension function may have an extra first argument of class <a class="bodylink" href="../../javadoc/net/sf/saxon/expr/XPathContext.html"><code>net.sf.saxon.expr.XPathContext</code></a>. This argument is not supplied by the calling XPath or XQuery code, but by Saxon itself. The <code>XPathContext</code> object provides methods to access many internal Saxon resources, the most useful being <code>getContextItem()</code> which returns the context item from the dynamic context. The XPathContext object is available with static or instance-level methods, but not with constructors.</p> <p>The following example shows a function that obtains the line number of the context node (this is actually a built-in Saxon extension):</p> <div class="codeblock" style="border: solid thin; background-color: #B1CCC7; padding: 2px"> <pre> <code> /** * Return the line number of the context node. */ public static int lineNumber(XPathContext c) { Item item = c.getCurrentIterator().current(); if (item instanceof NodeInfo) { return ((NodeInfo)item).getLineNumber(); } else { return -1; } }</code> </pre> </div> <p>If this method appears in class <code>com.example.code.NodeData</code>, then it can be accessed using the following code in XSLT:</p> <div class="codeblock" style="border: solid thin; background-color: #B1CCC7; padding: 2px"> <pre> <code><xsl:value-of select="nd:line-number()" xmlns:nd="java:com.example.code.NodeData"/></code> </pre> </div> <p>or the following in XQuery:</p> <div class="codeblock" style="border: solid thin; background-color: #B1CCC7; padding: 2px"> <pre> <code><line xmlns:nd="java:com.example.code.NodeData"> { nd:line-number() } </line></code> </pre> </div> <table width="100%"> <tr> <td> <p align="right"><a class="nav" href="constructors.xml">Next</a></p> </td> </tr> </table> </body> </html>