Home >>XQuery Tutorial >XQuery string-length() Function

XQuery string-length() Function

XQuery string-length() Function

The method XQuery string-length is used to determine the length of a string.

string-length($string as xs:string) as xs:integer

Parameter explanation:

$string: It specifies the provided string.

XQuery string-length Example

Let's take an example to illustrate the use of the string-length function of XQuery. Take an XQuery expression, named "books.xqy," with the code below.

XQuery Expression: books.xqy:


let $bookTitle := "Java Programming"  
let $size := string-length($bookTitle)  
return
<result>     
<size>{$size}</size>  
   </result>   

To read the books.xqy, build a Java-based XQuery executor application, transfer it over to the XQuery expression processor, 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("books.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 above both files to a same location. We put them on desktop in a folder name XQuery13. 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