xml - How to implement XQL Join using JAVA? -


i quite new xql , studied xql joins (join on xml documents) here :- http://www.ibiblio.org/xql/xql-proposal.html#joins . wondering if want implement xql join in java, how can using java ?

join new feature of xql. tried searching xql join examples in xquery , xpath apis in java haven't found (as of now) supoort xql join.

an example of xql join follows :-

suppose have source of books , source of reviews:-

<book>   <isbn> 84-7169-020-9 </isbn>   <title> tales of alhambra </title>   <author> washington irving </author> </book>  <review>   <isbn> 84-7169-020-9 </isbn>   <title> tales of alhambra </title>   <reviewer> ricardo sanchez </reviewer>   <comments>     romantic , humorous account of time     author of "the legend of sleepy hollow" lived     in arabian palace in spain.   </comments> </review> 

we may want combine these create view of book includes comments found in reviews:

<book>   <isbn> 84-7169-020-9 </isbn>   <title> tales of alhambra </title>   <author> washington irving </author>    <review>      <reviewer> ricardo sanchez </reviewer>      <comments>           romantic , humorous account of time           author of "the legend of sleepy hollow" lived           in arabian palace in spain.      </comments>   </review> </book> 

in order achieve that, can write following xql query :-

/book[isbn][$i:=isbn] {     $i  | title  | author     | //review[isbn=$i] { reviewer | comments } } 

though haven't found such support in xpath , xquery apis in java, still sake of trying, tried same using xpath java api. details follows :-

the xml kept in test file (books.txt), contains following data :-

<?xml version="1.0"?>  <catalog>    <book>     <isbn>0470192747</isbn>     <author>kay, michael</author>     <title>xslt 2.0 , xpath 2.0 (4th edition)</title>     <genre>computer</genre>     <price>33.99</price>     <publish_date>2008-06-03</publish_date>     <description>this book practical reference     book professional xslt developers.</description>   </book>    <book>     <isbn>0596006349</isbn>     <author>walmsley, priscilla</author>     <title>xquery</title>     <genre>computer</genre>     <price>38.50</price>     <publish_date>2007-03-30</publish_date>     <description>this in-depth tutorial not walks through     xquery specification, teaches how program     anticipated query language.</description>   </book>    <book>     <isbn>059652112x</isbn>     <author>kalin, martin</author>     <title>java web services: , running</title>     <genre>computer</genre>     <price>26.99</price>     <publish_date>2009-02-23</publish_date>     <description>with example-driven book, quick, practical,     , thorough introduction java's api xml web services (jax-ws)     , java api restful web services (jax-rs).</description>   </book>    <book>     <isbn>0321356683</isbn>     <author>bloch, joshua</author>     <title>effective java (2nd edition)</title>     <genre>computer</genre>     <price>35.99</price>     <publish_date>2008-05-22</publish_date>     <description>presents practical, authoritative guidelines     available writing efficient,well-designed programs.</description>   </book>    <book>     <isbn>0141014865</isbn>     <author>de botton, alain</author>     <title>status anxiety</title>     <genre>philosophy</genre>     <price>9.99</price>     <publish_date>2005-01-13</publish_date>     <description>the author presents universal condition of     many of suffer called status anxiety, investigates it's     origins , possible solutions.</description>   </book>    <book>     <isbn>0201771861</isbn>     <author>rusty harold, elliotte</author>     <title>processing xml java (sax, dom, jdom, jaxp &amp; trax)</title>     <genre>computer</genre>     <price>37.99</price>     <publish_date>2002-11-14</publish_date>     <description>handing , processing xml in     java programming language.</description>   </book>    <book>     <isbn>1887521143</isbn>     <author>poomsan becker, benjawan</author>     <title>thai-english , english-thai dictionary</title>     <genre>dictionary</genre>     <price>14.95</price>     <publish_date>2005-04-30</publish_date>     <description>with transliteration non-thai speakers -     complete thai alphabet guide</description>   </book>    <book>     <isbn>0415071771</isbn>     <author>jung, carl gustav</author>     <title>psychological types</title>     <genre>psychology</genre>     <price>19.99</price>     <publish_date>1992-01-02</publish_date>     <description>essential reading requiring proper     understanding of jung's psychology, work in jung     set out theory of psychological types means of understanding     ourselves , world around us.</description>   </book>    <book>     <isbn>0596003552</isbn>     <author>pawson, dave</author>     <title>xsl-fo: making xml in print</title>     <genre>computer</genre>     <price>26.99</price>     <publish_date>2002-08-19</publish_date>     <description>outlines xsl fo's strengths , weaknesses, provides     tutorial , reference guide.</description>   </book>    <book>     <isbn>0321392795</isbn>     <author>gray, simon</author>     <title>data structures in java</title>     <genre>computer</genre>     <price>53.99</price>     <publish_date>2006-11-13</publish_date>     <description>from abstract data types     java collections framework.</description>   </book>    <review>       <isbn>0321392795</isbn>       <title> tales of alhambra </title>       <reviewer> ricardo sanchez </reviewer>       <comments>         romantic , humorous account of time         author of "the legend of sleepy hollow" lived         in arabian palace in spain.       </comments>   </review>  </catalog> 

the java code implementation follows :-

import java.io.file; import java.io.ioexception;  import javax.xml.parsers.documentbuilderfactory; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.parserconfigurationexception; import javax.xml.xpath.xpath; import javax.xml.xpath.xpathconstants; import javax.xml.xpath.xpathexpressionexception; import javax.xml.xpath.xpathfactory;  import org.w3c.dom.document; import org.w3c.dom.nodelist; import org.w3c.dom.node; import org.w3c.dom.element; import org.xml.sax.saxexception;  public class xqljoin {     public static void main(string[] args) {        try {          file inputfile = new file("books.txt");          documentbuilderfactory dbfactory              = documentbuilderfactory.newinstance();          documentbuilder dbuilder;           dbuilder = dbfactory.newdocumentbuilder();           document doc = dbuilder.parse(inputfile);          doc.getdocumentelement().normalize();           xpath xpath =  xpathfactory.newinstance().newxpath();           string expression = "/catalog/book[isbn][$i:=isbn] {  $i  | title  | author | //review[isbn=$i] { reviewer | comments }}";                   nodelist nodelist = (nodelist) xpath.compile(expression).evaluate(doc, xpathconstants.nodeset);          (int = 0; < nodelist.getlength(); i++) {             node nnode = nodelist.item(i);             system.out.println("\ncurrent element :"                 + nnode.getnodename());             if (nnode.getnodetype() == node.element_node) {                element eelement = (element) nnode;               system.out.println("title : "                    + eelement                      .getelementsbytagname("title")                      .item(0)                      .gettextcontent());                system.out.println("reviewer : "                    + eelement                      .getelementsbytagname("reviewer")                      .item(0)                      .gettextcontent());             }          }       } catch (parserconfigurationexception e) {          e.printstacktrace();       } catch (saxexception e) {          e.printstacktrace();       } catch (ioexception e) {          e.printstacktrace();       } catch (xpathexpressionexception e) {          e.printstacktrace();       }    } } 

the above code threw exception following stacktrace :-

javax.xml.transform.transformerexception: expected ], found: isbn     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.error(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.consumeexpected(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.predicate(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.step(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.relativelocationpath(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.locationpath(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.pathexpr(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.unionexpr(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.unaryexpr(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.multiplicativeexpr(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.additiveexpr(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.relationalexpr(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.equalityexpr(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.andexpr(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.orexpr(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.expr(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.initxpath(unknown source)     @ com.sun.org.apache.xpath.internal.xpath.<init>(unknown source)     @ com.sun.org.apache.xpath.internal.xpath.<init>(unknown source)     @ com.sun.org.apache.xpath.internal.jaxp.xpathimpl.compile(unknown source)     @ xqljoin.main(xqljoin.java:36) --------------- linked ------------------ javax.xml.xpath.xpathexpressionexception: javax.xml.transform.transformerexception: expected ], found: isbn     @ com.sun.org.apache.xpath.internal.jaxp.xpathimpl.compile(unknown source)     @ xqljoin.main(xqljoin.java:36) caused by: javax.xml.transform.transformerexception: expected ], found: isbn     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.error(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.consumeexpected(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.predicate(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.step(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.relativelocationpath(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.locationpath(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.pathexpr(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.unionexpr(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.unaryexpr(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.multiplicativeexpr(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.additiveexpr(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.relationalexpr(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.equalityexpr(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.andexpr(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.orexpr(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.expr(unknown source)     @ com.sun.org.apache.xpath.internal.compiler.xpathparser.initxpath(unknown source)     @ com.sun.org.apache.xpath.internal.xpath.<init>(unknown source)     @ com.sun.org.apache.xpath.internal.xpath.<init>(unknown source)     ... 2 more 

please suggest how implement functionality using java. clues ?

first let's start xql; xql obsolete, far know there no current implementations, believe xql pre-cursor xquery, should starting xquery 3.1 rather worrying xql.

that being said, xquery lacks explicit joins described in xql. xquery have facility creating joins, there no implicit merge of results. can explicitly merge results using 1 of 3 methods come mind:

  1. xquery update take result of first query , insert result of join.
  2. an identity transform on result of first query, injecting result of join.
  3. using element-constructors output shape of xml want , placing queries structure (shown below).

the problem code above xpath invalid, error receiving telling you, i.e. [$i:=isbn] initial problem, {{ part isn't valid xpath or xquery either.

for xquery expression looking for, present perhaps easier approach beginners (i.e. option 3 above):

<catalog> {     $book in /catalog/book     let $reviews := /catalog/review[isbn eq $book/isbn]     return         <book>         {             $book/*,             $review in $reviews             return                 <review>                 {                     $review/(reviewer | comments)                 }                 </review>         }         </book> } </catalog> 

this produce following output (from source document described in question):

<?xml version="1.0" encoding="utf-8"?> <catalog>    <book>       <isbn>0470192747</isbn>       <author>kay, michael</author>       <title>xslt 2.0 , xpath 2.0 (4th edition)</title>       <genre>computer</genre>       <price>33.99</price>       <publish_date>2008-06-03</publish_date>       <description>this book practical reference             book professional xslt developers.</description>    </book>    <book>       <isbn>0596006349</isbn>       <author>walmsley, priscilla</author>       <title>xquery</title>       <genre>computer</genre>       <price>38.50</price>       <publish_date>2007-03-30</publish_date>       <description>this in-depth tutorial not walks through             xquery specification, teaches how program             anticipated query language.</description>    </book>    <book>       <isbn>059652112x</isbn>       <author>kalin, martin</author>       <title>java web services: , running</title>       <genre>computer</genre>       <price>26.99</price>       <publish_date>2009-02-23</publish_date>       <description>with example-driven book, quick, practical,             , thorough introduction java's api xml web services (jax-ws)             , java api restful web services (jax-rs).</description>    </book>    <book>       <isbn>0321356683</isbn>       <author>bloch, joshua</author>       <title>effective java (2nd edition)</title>       <genre>computer</genre>       <price>35.99</price>       <publish_date>2008-05-22</publish_date>       <description>presents practical, authoritative guidelines             available writing efficient,well-designed programs.</description>    </book>    <book>       <isbn>0141014865</isbn>       <author>de botton, alain</author>       <title>status anxiety</title>       <genre>philosophy</genre>       <price>9.99</price>       <publish_date>2005-01-13</publish_date>       <description>the author presents universal condition of             many of suffer called status anxiety, investigates it's             origins , possible solutions.</description>    </book>    <book>       <isbn>0201771861</isbn>       <author>rusty harold, elliotte</author>       <title>processing xml java (sax, dom, jdom, jaxp &amp; trax)</title>       <genre>computer</genre>       <price>37.99</price>       <publish_date>2002-11-14</publish_date>       <description>handing , processing xml in             java programming language.</description>    </book>    <book>       <isbn>1887521143</isbn>       <author>poomsan becker, benjawan</author>       <title>thai-english , english-thai dictionary</title>       <genre>dictionary</genre>       <price>14.95</price>       <publish_date>2005-04-30</publish_date>       <description>with transliteration non-thai speakers -             complete thai alphabet guide</description>    </book>    <book>       <isbn>0415071771</isbn>       <author>jung, carl gustav</author>       <title>psychological types</title>       <genre>psychology</genre>       <price>19.99</price>       <publish_date>1992-01-02</publish_date>       <description>essential reading requiring proper             understanding of jung's psychology, work in jung             set out theory of psychological types means of understanding             ourselves , world around us.</description>    </book>    <book>       <isbn>0596003552</isbn>       <author>pawson, dave</author>       <title>xsl-fo: making xml in print</title>       <genre>computer</genre>       <price>26.99</price>       <publish_date>2002-08-19</publish_date>       <description>outlines xsl fo's strengths , weaknesses, provides             tutorial , reference guide.</description>    </book>    <book>       <isbn>0321392795</isbn>       <author>gray, simon</author>       <title>data structures in java</title>       <genre>computer</genre>       <price>53.99</price>       <publish_date>2006-11-13</publish_date>       <description>from abstract data types             java collections framework.</description>       <review>          <reviewer> ricardo sanchez </reviewer>          <comments>             romantic , humorous account of time             author of "the legend of sleepy hollow" lived             in arabian palace in spain.         </comments>       </review>    </book> </catalog> 

if use expression above, should go :-)


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -