2016-05-09 3 views
0

私はXQLにはかなり新しく、ここではXQL結合(XML文書への結合)を学んでいます: - http://www.ibiblio.org/xql/xql-proposal.html#Joins。私は、JavaでXQL Joinを実装したいのであれば、どうすればJAVAを使ってそれを行うことができますか?JAVAを使用してXQL結合を実装する方法は?

結合は、XQLの新機能です。私は、XQueryとJavaのXPath APIのXQL Joinサンプルを検索しようとしましたが、XQL Joinをサポートするためのものはまだ見つかりませんでした。次のように

XQLの例にジョインは次のとおりです。 -

は、私たちが図書のソースとレビューのソースを持っていると仮定します -

<book> 
    <isbn> 84-7169-020-9 </isbn> 
    <title> Tales of the Alhambra </title> 
    <author> Washington Irving </author> 
</book> 

<review> 
    <isbn> 84-7169-020-9 </isbn> 
    <title> Tales of the Alhambra </title> 
    <reviewer> Ricardo Sanchez </reviewer> 
    <comments> 
    A romantic and humorous account of the time that 
    the author of "The Legend of Sleepy Hollow" lived 
    in an Arabian palace in Spain. 
    </comments> 
</review> 

我々はのビューを作成するには、これらを結合する場合があります

- :レビューで見つかったコメントが含まブック:

<book> 
    <isbn> 84-7169-020-9 </isbn> 
    <title> Tales of the Alhambra </title> 
    <author> Washington Irving </author> 

    <review> 
    <reviewer> Ricardo Sanchez </reviewer> 
    <comments> 
      A romantic and humorous account of the time that 
      the author of "The Legend of Sleepy Hollow" lived 
      in an Arabian palace in Spain. 
    </comments> 
    </review> 
</book> 

それを達成するために、我々は以下のXQLクエリを書くことができます

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

私はJAVAのXPath APIとXQuery APIでこのようなサポートを見つけられませんでしたが、まだ試してみるためにXPath JAVA APIを使用して同じことを試みました。詳細は以下のとおり -

XMLは、次のデータを含むこと、テストファイル(books.txt)に保持されている: -

<?xml version="1.0"?> 

<catalog> 

    <book> 
    <isbn>0470192747</isbn> 
    <author>Kay, Michael</author> 
    <title>XSLT 2.0 and XPath 2.0 (4th Edition)</title> 
    <genre>Computer</genre> 
    <price>33.99</price> 
    <publish_date>2008-06-03</publish_date> 
    <description>This book is primarily a practical reference 
    book for 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 only walks you through 
    the XQuery specification, but also teaches you how to program with 
    this widely anticipated query language.</description> 
    </book> 

    <book> 
    <isbn>059652112X</isbn> 
    <author>Kalin, Martin</author> 
    <title>Java Web Services: Up and Running</title> 
    <genre>Computer</genre> 
    <price>26.99</price> 
    <publish_date>2009-02-23</publish_date> 
    <description>With this example-driven book, you get a quick, practical, 
    and thorough introduction to Java's API for XML Web Services (JAX-WS) 
    and the Java API for 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 the most practical, authoritative guidelines 
    available for 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 a universal condition of which 
    many of us suffer from called Status Anxiety, investigates it's 
    origins and possible solutions.</description> 
    </book> 

    <book> 
    <isbn>0201771861</isbn> 
    <author>Rusty Harold, Elliotte</author> 
    <title>Processing XML with Java (SAX, DOM, JDOM, JAXP &amp; TrAX)</title> 
    <genre>Computer</genre> 
    <price>37.99</price> 
    <publish_date>2002-11-14</publish_date> 
    <description>Handing and processing XML in 
    the Java programming language.</description> 
    </book> 

    <book> 
    <isbn>1887521143</isbn> 
    <author>Poomsan Becker, Benjawan</author> 
    <title>Thai-English and English-Thai Dictionary</title> 
    <genre>Dictionary</genre> 
    <price>14.95</price> 
    <publish_date>2005-04-30</publish_date> 
    <description>With Transliteration for Non-Thai Speakers - 
    Complete with 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 for anyone requiring a proper 
    understanding of Jung's psychology, this was the work in which Jung 
    set out his theory of psychological types as a means of understanding 
    ourselves and the world around us.</description> 
    </book> 

    <book> 
    <isbn>0596003552</isbn> 
    <author>Pawson, Dave</author> 
    <title>XSL-FO: Making XML Look Good in Print</title> 
    <genre>Computer</genre> 
    <price>26.99</price> 
    <publish_date>2002-08-19</publish_date> 
    <description>Outlines XSL FO's strengths and weaknesses, provides 
    a tutorial and 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 to the 
    Java Collections Framework.</description> 
    </book> 

    <review> 
     <isbn>0321392795</isbn> 
     <title> Tales of the Alhambra </title> 
     <reviewer> Ricardo Sanchez </reviewer> 
     <comments> 
     A romantic and humorous account of the time that 
     the author of "The Legend of Sleepy Hollow" lived 
     in an Arabian palace in Spain. 
     </comments> 
    </review> 

</catalog> 

次のように実装するためのJavaコードである: -

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); 
     for (int i = 0; i < 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(); 
     } 
    } 
} 

上記のコードは、次のスタックトレースと例外がスローされました: -

javax.xml.transform.TransformerException: Expected ], but found: isbn 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.error(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.consumeExpected(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.Predicate(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.Step(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelativeLocationPath(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.LocationPath(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.PathExpr(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnionExpr(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnaryExpr(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.MultiplicativeExpr(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.AdditiveExpr(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelationalExpr(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.EqualityExpr(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.AndExpr(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.OrExpr(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.Expr(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(Unknown Source) 
    at com.sun.org.apache.xpath.internal.XPath.<init>(Unknown Source) 
    at com.sun.org.apache.xpath.internal.XPath.<init>(Unknown Source) 
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(Unknown Source) 
    at XQLJoin.main(XQLJoin.java:36) 
--------------- linked to ------------------ 
javax.xml.xpath.XPathExpressionException: javax.xml.transform.TransformerException: Expected ], but found: isbn 
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(Unknown Source) 
    at XQLJoin.main(XQLJoin.java:36) 
Caused by: javax.xml.transform.TransformerException: Expected ], but found: isbn 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.error(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.consumeExpected(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.Predicate(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.Step(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelativeLocationPath(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.LocationPath(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.PathExpr(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnionExpr(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnaryExpr(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.MultiplicativeExpr(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.AdditiveExpr(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelationalExpr(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.EqualityExpr(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.AndExpr(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.OrExpr(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.Expr(Unknown Source) 
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(Unknown Source) 
    at com.sun.org.apache.xpath.internal.XPath.<init>(Unknown Source) 
    at com.sun.org.apache.xpath.internal.XPath.<init>(Unknown Source) 
    ... 2 more 

してくださいJavaを使用してこの機能を実装する方法を提案します。すべての手がかりは?

答えて

1

まず、XQLで始めましょう。 XQLは現在のところ実装されていない限り、XQLはXQueryのプリカーソルだったと思うので、XQLについて心配するよりも、実際にはXQuery 3.1から始めるべきです。

XQueryには、XQLで説明されているの明示的なジョインが欠けています。 XQueryには結合を作成する機能がありますが、結果の暗黙的なマージはありません。最初のクエリの結果を取り、結合の結果を挿入するために

  1. XQuery Update:あなたは、明示的にすぐに頭に浮かぶ3つの方法の1を使用して結果をマージすることができます。
  2. identity transform最初のクエリの結果で、結合の結果を挿入します。
  3. 要素コンストラクタを使用して、必要なXMLの形を出力し、その構造にクエリを配置します(下図参照)。

上記のコードに問題があなたのXPathは正確にあなたがされるエラーがあなたを語っているものである、無効であるということである、すなわち[$i:=isbn]は最初の問題であるが、その後{{部分は、有効なXPathかではありませんいずれかのXQuery。あなたが探しているものを行うには、XQuery式の場合

、私は初心者のために、おそらく(上からすなわちオプション3)を簡単にアプローチを提示します:

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

これは以下の出力を生成します(ソースからあなたは、あなたの質問に記述された文書):

<?xml version="1.0" encoding="UTF-8"?> 
<catalog> 
    <book> 
     <isbn>0470192747</isbn> 
     <author>Kay, Michael</author> 
     <title>XSLT 2.0 and XPath 2.0 (4th Edition)</title> 
     <genre>Computer</genre> 
     <price>33.99</price> 
     <publish_date>2008-06-03</publish_date> 
     <description>This book is primarily a practical reference 
      book for 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 only walks you through 
      the XQuery specification, but also teaches you how to program with 
      this widely anticipated query language.</description> 
    </book> 
    <book> 
     <isbn>059652112X</isbn> 
     <author>Kalin, Martin</author> 
     <title>Java Web Services: Up and Running</title> 
     <genre>Computer</genre> 
     <price>26.99</price> 
     <publish_date>2009-02-23</publish_date> 
     <description>With this example-driven book, you get a quick, practical, 
      and thorough introduction to Java's API for XML Web Services (JAX-WS) 
      and the Java API for 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 the most practical, authoritative guidelines 
      available for 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 a universal condition of which 
      many of us suffer from called Status Anxiety, investigates it's 
      origins and possible solutions.</description> 
    </book> 
    <book> 
     <isbn>0201771861</isbn> 
     <author>Rusty Harold, Elliotte</author> 
     <title>Processing XML with Java (SAX, DOM, JDOM, JAXP &amp; TrAX)</title> 
     <genre>Computer</genre> 
     <price>37.99</price> 
     <publish_date>2002-11-14</publish_date> 
     <description>Handing and processing XML in 
      the Java programming language.</description> 
    </book> 
    <book> 
     <isbn>1887521143</isbn> 
     <author>Poomsan Becker, Benjawan</author> 
     <title>Thai-English and English-Thai Dictionary</title> 
     <genre>Dictionary</genre> 
     <price>14.95</price> 
     <publish_date>2005-04-30</publish_date> 
     <description>With Transliteration for Non-Thai Speakers - 
      Complete with 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 for anyone requiring a proper 
      understanding of Jung's psychology, this was the work in which Jung 
      set out his theory of psychological types as a means of understanding 
      ourselves and the world around us.</description> 
    </book> 
    <book> 
     <isbn>0596003552</isbn> 
     <author>Pawson, Dave</author> 
     <title>XSL-FO: Making XML Look Good in Print</title> 
     <genre>Computer</genre> 
     <price>26.99</price> 
     <publish_date>2002-08-19</publish_date> 
     <description>Outlines XSL FO's strengths and weaknesses, provides 
      a tutorial and 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 to the 
      Java Collections Framework.</description> 
     <review> 
     <reviewer> Ricardo Sanchez </reviewer> 
     <comments> 
      A romantic and humorous account of the time that 
      the author of "The Legend of Sleepy Hollow" lived 
      in an Arabian palace in Spain. 
     </comments> 
     </review> 
    </book> 
</catalog> 

あなたは上記のごexpressionとしてこれを使用する場合は、

:-)行くために良いことがあります
関連する問題