IBM webMethods Hybrid Integration

IBM webMethods Hybrid Integration

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
  • 1.  Querying an XML Document with XQuery

    Posted Tue May 16, 2006 06:32 PM

    Hello, I would like to use XQuery to pose queries on an XML document using Java (JDOM). Which are the tools and the techniques necessary for that? If it is possible, I will like a document which treats an example on how using and testin Xquery with an XML document, the whole with Java.

    thanks in advance. :slight_smile:


    #webMethods
    #API-Management
    #Tamino


  • 2.  RE: Querying an XML Document with XQuery

    Posted Tue May 16, 2006 07:02 PM

    Hi,

    You can use Tamino processor to query within an XML Document.

    for example…

    
    for $q in (<Root>
    <Hello>Hi! </Hello>
    <Name>Nice</Name>
    </Root>)
    return <HelloExample>{xs:string($q/Hello)} {xs:string($q/Name)}</HelloExample>

    the result is:

     <HelloExample>Hi! Nice</HelloExample>

    You can use Java to build the Query string and the Tamino API to process it.

    Also, you can use SAXON, http://saxon.sourceforge.net


    #webMethods
    #Tamino
    #API-Management


  • 3.  RE: Querying an XML Document with XQuery

    Posted Wed May 17, 2006 02:52 PM

    Thank u Claudio ! :slight_smile:
    please how can I integrate the queries in the java code ? for example, with Saxon ?


    #webMethods
    #API-Management
    #Tamino


  • 4.  RE: Querying an XML Document with XQuery

    Posted Wed May 17, 2006 04:32 PM

    Hi,

    Checking the following link out you can start working with SAXON and XQuery.

    http://saxon.sourceforge.net/saxon7.9/using-xquery.html#Embedding

    A quick example:

    /*
    * Xquery.java
    * 
    * Created on May 17, 2006 by cdelgado
    * 
    */
    package cl.softwareag.test;
    
    import net.sf.saxon.Configuration;
    import net.sf.saxon.om.NodeInfo;
    import net.sf.saxon.query.DynamicQueryContext;
    import net.sf.saxon.query.StaticQueryContext;
    import net.sf.saxon.query.XQueryExpression;
    import net.sf.saxon.trans.XPathException;
    
    public class Xquery {
    
    public static void main(String[] args) {
    
    Configuration conf = new Configuration();
    StaticQueryContext staticContext = new StaticQueryContext(conf);
    
    String query = "for $q in (<Root> <Hello>Hi! "
    + "</Hello>   <Name>Nice</Name>   </Root>) "
    + "return <HelloExample>{xs:string($q/Hello)} "
    + "{xs:string($q/Name)}</HelloExample> ";
    
    try {
    XQueryExpression xqe = staticContext.compileQuery(query);
    DynamicQueryContext dc = new DynamicQueryContext(conf);
    NodeInfo ob = (NodeInfo) xqe.evaluateSingle(dc);
    System.out.print(ob.getRoot());
    // It prints Hi! Nice in console
    } catch (XPathException e) {
    e.printStackTrace();
    }
    
    }
    
    }

    #Tamino
    #webMethods
    #API-Management


  • 5.  RE: Querying an XML Document with XQuery

    Posted Sun May 21, 2006 01:50 PM

    Thank u Claudio. :slight_smile:
    I wrote this class to test XQuery request with the SAXON processor. But it always posts me an error of compilation in the class Configuration, although all my classes all are under the same repertory as the class Xquery hand (in java/jdk1.5/bin).

    Xquery.java

    
    import java.io.*; 
    
    import Configuration.java;
    import NodeInfo.java;
    import DynamicQueryContext.java;
    import StaticQueryContext.java;
    import XQueryExpression.java;
    import XPathException.java;
    
    public class Xquery {
    
    public static void main(String[] args) {
    
    Configuration conf = new Configuration();
    StaticQueryContext staticContext = new StaticQueryContext(conf);
    
    
    String query = "for $q in (<Root> <Hello>Hi! "
    + "</Hello>   <Name>Nice</Name>   </Root>) "
    + "return <HelloExample>{xs:string($q/Hello)} "
    + "{xs:string($q/Name)}</HelloExample> ";
    
    
    //QueryProcessor qp = new QueryProcessor(conf, staticContext);
    
    try {
    XQueryExpression xqe = staticContext.compileQuery(query);
    DynamicQueryContext dc = new DynamicQueryContext(conf);
    NodeInfo ob = (NodeInfo) xqe.evaluateSingle(dc);
    
    System.out.print(ob.getRoot());
    // It prints Hi! Nice in console
    } catch (XPathException e) {
    e.printStackTrace();
    }
    }
    }

    [/b]


    #API-Management
    #Tamino
    #webMethods