Sophie

Sophie

distrib > Mageia > 7 > armv7hl > by-pkgid > b3bdfe6d859a3d6920ff2c44b38e9a6f > files > 271

saxon-manual-9.4.0.9-2.mga7.noarch.rpm

<?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>&lt;xsl:value-of select="math:sqrt($arg)"
   xmlns:math="java:java.lang.Math"/&gt;
</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>&lt;a xmlns:double="java:java.lang.Double"&gt; 
                              {double:MAX_VALUE()} &lt;/a&gt;
</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>&lt;xsl:value-of select="nd:line-number()" 
    xmlns:nd="java:com.example.code.NodeData"/&gt;</code>
         </pre>
      </div>
      <p>or the following in XQuery:</p>
      <div class="codeblock"
           style="border: solid thin; background-color: #B1CCC7; padding: 2px">
         <pre>
            <code>&lt;line xmlns:nd="java:com.example.code.NodeData"&gt;
    { nd:line-number() }
&lt;/line&gt;</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>