- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 XQL 很陌生,最近在这里研究了 XQL 连接(连接 XML 文档):- http://www.ibiblio.org/xql/xql-proposal.html#Joins 。我只是想知道,如果我想在 JAVA 中实现 XQL Join,我该如何使用 JAVA 来实现呢?
加入是 XQL 的一项新功能。我尝试在 JAVA 中的 XQuery 和 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>
我们可能希望将这些结合起来创建一个包含评论中评论的图书 View :
<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 和 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 & 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 实现此功能。有什么线索吗?
最佳答案
首先让我们从 XQL 开始; XQL 已过时,据我所知当前没有实现,我相信 XQL 是 XQuery 的前身,所以您应该从 XQuery 3.1 开始而不是担心 XQL。
话虽如此,XQuery 缺乏 XQL 中描述的显式连接。 XQuery 确实具有创建连接的功能,但没有隐式的结果合并。您可以使用立即想到的 3 种方法之一来显式合并结果:
上面代码的问题是您的 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 & 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>
如果您使用它作为上面的表达式
,那么您应该可以开始了:-)
关于java - 如何使用JAVA实现XQL Join?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37112599/
我正在测试设置SQLAlchemy以映射现有数据库。这个数据库是很久以前自动建立的,它是由我们不再使用的先前的第三方应用程序创建的,因此 undefined 某些预期的事情,例如外键约束。该软件将管理
这个问题在这里已经有了答案: What is the difference between "INNER JOIN" and "OUTER JOIN"? (28 个答案) 关闭 7 年前。 INNE
这个问题在这里已经有了答案: What is the difference between "INNER JOIN" and "OUTER JOIN"? (29 个回答) 关闭7年前. INNER J
假设有两个表: table1.c1 table1.c2 1 1 A 2 1 B 3 1 C 4 2
假设有两个表: table1.c1 table1.c2 1 1 A 2 1 B 3 1 C 4 2
一.先看一些最简单的例子 例子 Table A aid adate 1 a1 2&nb
数据库操作语句 7. 外连接——交叉查询 7.1 查询 7.2 等值连接 7.3 右外
我有两个表 'users' 和 'lms_users' class LmsUser belongs_to :user end class User has_one :lms_user
我试图避免在 Rails 中对我的 joins 进行字符串插值,因为我注意到将查询器链接在一起时灵活性会降低。 也就是说,我觉得 joins(:table1) 比 joins('inner join
我有这个代码 User.find(:all, :limit => 10, :joins => :user_points, :select => "users.*, co
我刚刚开始探索 Symfony2,我很惊讶它拥有如此多的强大功能。我开始做博客教程在: http://tutorial.symblog.co.uk/ 但使用的是 2.1 版而不是 2.0 我的问题是我
什么是 SQL JOIN什么是不同的类型? 最佳答案 插图来自 W3schools : 关于SQL JOIN 和不同类型的 JOIN,我们在Stack Overflow上找到一个类似的问题: http
我有两个 Hive 表,我正在尝试加入它们。这些表没有被任何字段聚集或分区。尽管表包含公共(public)键字段的记录,但连接查询始终返回 0 条记录。所有数据类型都是“字符串”数据类型。 连接查询很
我正在使用 Solr 的(4.0.0-beta)连接功能来查询包含具有父/子关系的文档的索引。连接查询效果很好,但我只能在搜索结果中获得父文档。我相信这是预期的行为。 但是,是否有可能在搜索结果中同时
我正在使用可用的指南/api/书籍自学 Rails,但我无法理解通过三种方式/嵌套 has_many :through 关联进行的连接。 我有用户与组相关联:通过成员(member)资格。 我在多对多
什么是 SQL JOIN,有哪些不同的类型? 最佳答案 插图来自 W3schools : 关于SQL JOIN 和不同类型的 JOIN,我们在Stack Overflow上找到一个类似的问题: htt
我正在尝试访问数据库的两个表。在商店里,我保留了一个事件列表,其中包含 Table Event id, name,datei,houri, dateF,Hourf ,capacity, age ,de
我有 4 个表:booking、address、search_address 和 search_address_log 表:(相关列) 预订:(pickup_address_id, dropoff_a
我在YML中有以下结构:。我正试着创造一个这样的结构:。作业名称和脚本用~分隔,作业用;分隔。。我可以使用以下命令使其正常工作。然而,我想知道是否可以用一个yq表达式来完成,而不是通过管道再次使用yq
我在YML中有以下结构:。我正试着创造一个这样的结构:。作业名称和脚本用~分隔,作业用;分隔。。我可以使用以下命令使其正常工作。然而,我想知道是否可以用一个yq表达式来完成,而不是通过管道再次使用yq
我是一名优秀的程序员,十分优秀!