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

XQuery current-date() Function

XQuery current-date() Function

The current date feature in XQuery is used to find the latest date.

current-date()

XQuery current-date() Example

To get the latest date using the following XQuery expression.

current-date.xqy:


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

Build a Java-based XQuery executor software to interpret the current-date.xqy, move it on to the processor for XQuery expression, 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-date.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 XQuery10 on 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