Home >>XQuery Tutorial >XQuery current-time() Function

XQuery current-time() Function

XQuery current-time() Function

The current-time XQuery function is used to provide the current time.

current-time()

XQuery current-time() Example

Use the following XQuery expression to fetch the current time.

current-time.xqy:


let $time := current-time()  
return  
<results>  
   <time>{$time}</time>  
</results>

Build a Java dependent XQuery executor program to interpret the current-time.xqy, pass it on to the processor for XQuery input, and execute the expression. After that it shows the output.

XQueryTester.java


import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.InputStream;  
  
import javax.xml.xquery.XQConnection;  
import javax.xml.xquery.XQDataSource;  
import javax.xml.xquery.XQException;  
import javax.xml.xquery.XQPreparedExpression;  
import javax.xml.xquery.XQResultSequence;  
  
import com.saxonica.xqj.SaxonXQDataSource;  
  
public class XQueryTester {  
   public static void main(String[] args){  
      try {  
         execute();  
      }  
        
      catch (FileNotFoundException e) {  
         e.printStackTrace();  
      }  
        
      catch (XQException e) {  
         e.printStackTrace();  
      }  
   }  
  
   private static void execute() throws FileNotFoundException, XQException{  
      InputStream inputStream = new FileInputStream(new File("current-time.xqy"));  
      XQDataSource ds = new SaxonXQDataSource();  
      XQConnection conn = ds.getConnection();  
      XQPreparedExpression exp = conn.prepareExpression(inputStream);  
      XQResultSequence result = exp.executeQuery();  
       while (result.next()) {  
         System.out.println(result.getItemAsString(null));  
      }  
   }      
}  

Execute XQuery against XML

Put the two files above to the same location. We put them in a folder called XQuery11 on your desktop. Use terminal compile XQueryTester.java. You require JDK 1.5 or later to be enabled on your computer, and optimized classpaths.

Compile:

javac XQueryTester.java

Execute:

java XQueryTester

No Sidebar ads