以文本方式查看主题 - 中文XML论坛 - 专业的XML技术讨论区 (http://bbs.xml.org.cn/index.asp) -- 『 XQuery/XLink/XPointer/ 』 (http://bbs.xml.org.cn/list.asp?boardid=14) ---- 看来搞xquery的人真的不多!贴个入门帖吧 (http://bbs.xml.org.cn/dispbbs.asp?boardid=14&rootid=&id=24912) |
-- 作者:hama -- 发布时间:12/2/2005 12:34:00 PM -- 看来搞xquery的人真的不多!贴个入门帖吧 This article is for all those people who really want to know what XQuery is, but don't have the time to find out. We all know the problem: so many exciting new technologies, so little time to research them. To be honest, I hope that you'll spend more than ten minutes on it — but if you really have to leave that soon, I hope you'll learn something useful anyway. What is XQuery For? Some people are also using XQuery for manipulating free-standing XML documents, for example, for transforming messages passing between applications. In that role XQuery competes directly with XSLT, and which language you choose is largely a matter of personal preference. In fact, some people like XQuery so much that they are even using it for rendering XML into HTML for presentation. That's not really the job XQuery was designed for, and I wouldn't recommend people to do that, but once you get to know a tool, you tend to find new ways of using it. Playing with XQuery (Between you and me, if you've only got ten minutes, you're not going to have time to install any new software, so just keep reading ...) Your First XQuery and this is the result: This is how it works in Stylus Studio®:
Game for something more interesting? Try: and be amazed by the answer: Finally, just to check that things are working properly, enter: and you'll see how much time you have left to read the rest of this article: For that one, of course, mileage may vary. The precision of the time value (fractions of a second) depends on the XQuery processor you are using, and the timezone (5 hours before GMT in this case) depends on how your system is configured. None of these is a very useful query on its own, of course, and what they demonstrate isn't exactly rocket science. But within a query language, you need to be able to do little calculations, and XQuery has this covered. Further, XQuery is designed so that expressions are fully nestable — any expression can be used within any other expression, provided it delivers a value of the right type — and this means that expressions that are primarily intended for selecting data within a where clause can also be used as free-standing queries in their own right. Accessing XML Documents with XQuery The source document we'll use is called videos.xml. It's distributed as an example file with Stylus Studio®, and you'll find it somewhere like c:\Program Files\Stylus Studio® 6 XML Professional Edition\examples\VideoCenter\videos.xml There's also a copy of this example file on the Web. XQuery allows you to access the file directly from either of these locations, using a suitable URL as an argument to its doc() function. Here's an XQuery that simply retrieves and displays the whole document: The same function can be used to get the copy from the Web: (This will only work if you are online, of course; and if you're behind a corporate firewall you may have to do some tweaking of your Java configuration to make it work.) Those URLs are a bit unwieldy, but there are shortcuts you can use: The file contains a number of sections. One of them is an <actors> element, which we can select like this: This produces the result: This means we can also write more complex XPath expressions like this one: which gives the output:
This example used another function — ends-with() — that's new in XPath 2.0. We're calling it inside a predicate (the expression between the square brackets), which defines a condition that nodes must satisfy in order to be selected. This XPath expression has two parts: a path .//actors/actor that indicates which elements we are interested in, and a predicate [ends-with(., 'Lisa')] that indicates a test that the nodes must satisfy. The predicate is evaluated once for each selected element; within the predicate, the expression "." (dot) refers to the node that the predicate is testing, that is, the selected actor. The "/" in the path informally means "go down one level", while the "//" means "go down any number of levels". If the path starts with "./" or ".//" you can leave out the initial "." (this assumes that the selection starts from the top of the tree, which is always the case in our examples). You can also use constructs like "/.." to go up one level, and "/@id" to select an attribute. Again, this will all be familiar if you already know XPath. XPath is capable of doing some pretty powerful selections, and before we move on to XQuery proper, let's look at a more complex example. Let's suppose we want to find the titles of all the videos featuring an actor whose first name is Lisa. Each video in the file is represented by a video element like this one: We can write the required query like this: Again, this is pure XPath (and therefore a valid XQuery). You can read it from left-to-right as: The result is: XQuery FLWOR Expressions And of course, we get the same result. Let's take apart this FLWOR expression: If you've been following very closely, you might have noticed one little XPath trick that we've retained in this query. Most videos will feature more than one actor (though this particular database doesn't attempt to catalog the bit-part players). The expression $v/actorRef therefore selects several elements. The rules for the = operator in XPath (and therefore also in XQuery) are that it compares everything on the left with everything on the right and returns true if there's at least one match. In effect, it's doing an implicit join. If you want to avoid exploiting this feature, and to write your query in a more classically relational form, you could express it as: This time I've used a different equality operator, eq, which follows more conventional rules than = does: it strictly compares one value on the left with one value on the right. (But like comparisons in SQL, it has special rules to handle the case where one of the values is absent.) What about the O in FLWOR? That's there so you can get the results in sorted order. Suppose you want the videos in order of their release date. Here's the revised query: And if you're wondering why it isn't a LFWOR expression: the for and let clauses can appear in any order, and you can have any number of each. That, and LFWOR doesn't exactly fall trippingly off the tongue, now does it?. There's much more to the FLOWR expression then what's covered in this brief XQuery tutorial — for more information be sure to check out the XQuery FLWOR tutorial. Generating XML Output with XQuery XQuery allows the structure of the result document to be defined using an XML-like notation. Here's an example that fleshes out our previous query with some XML markup: I've also changed the query so that the actor's first name is now a parameter. This makes the query reusable. The way parameters are supplied varies from one XQuery processor to another. In Stylus Studio®, select XQuery > Scenario Properties; click the Parameter Values tab, and you'll see a space to enter the parameter value. Enter "Lisa", in quotes (Stylus Studio® expects an expression, so if you leave out the quotes, this value would be taken as a reference to an element named <Lisa>). If instead you're running Saxon from the command line, you can enter: This is how the output looks now: (Not a very well-designed query, since the two videos feature different actresses both named Lisa; but if your ten minutes aren't up yet, perhaps you can improve it yourself.) Show Me the Database! People sometimes squeeze a large data set (for example, a corporate phone directory) into a single XML document, and process it as a file without the benefit of any database system. It's not something I'd particularly recommend, but if the data volumes don't go above a few megabytes and the transaction rate is modest, then it's perfectly feasible. So the examples in this introduction aren't totally unrealistic. If you've got a real database, however, the form of the queries won't need to change all that much from these examples. Instead of using the doc() function (or simply ".") to select a document, you're likely to call the collection() function to open a database, or a specific collection of documents within a database. The actual way collections are named is likely to vary from one database system to another. The result of the XQuery collection() function is a set of documents (more strictly, a sequence of documents, but the order is unlikely to matter), and you can process this using path expressions or FLWOR expressions in just the same way as you address a single document. There's a lot more to databases than doing queries, of course. Each product has its own ways of setting up the database, defining schemas, loading documents, and performing maintenance operations such as backup and recovery. XQuery currently handles only one small part of the job. In the future it's also likely to have an update capability, but in the meantime each vendor is defining his own. One particularly nice feature of XQuery is that it has the potential to combine data from multiple databases (and freestanding XML documents). If that's something you're interested in, take a look at DataDirect XQuery™, which supports access to Oracle, DB2, SQL Server and Sybase. Time's Up! Can't wait that long? Then check out the XQuery tutorial written by Jonathan Robie of DataDirect Technologies™, the first chapter of the book XQuery from the Experts, which provides an in-depth understanding of the design behind the XQuery language. The book also contains a chapter by the author of this article, Michael Kay, about the relationship of XQuery to XSLT. It's a great way to get your feet wet with this powerful new XML technology. If you want to get your hands dirty right away, Stylus Studio® provides a ton of XQuery tools, including an XQuery editor, an XQuery Debugger with integrated support for the Saxon XQuery Processor, an XQuery Mapper for visually developing XQuery projects, and an XQuery Profiler for benchmarking and optimizing XQuery expressions. Best of all, Stylus Studio® provides several online video demonstrations to get you aquainted with these and other tools, and you can try out Stylus Studio® for free. Finally, if you're more academically inclined, you can find the XQuery specification itself at http://www.w3.org/TR/XQuery. As standards documents go, it's actually quite readable, and it has lots of examples. The specification is part of a raft of documents on XQuery, which are all listed in its References section, but the one you're likely to find especially useful is the Functions and Operators specification at http://www.w3.org/TR/xpath-functions. This document lists all the functions in the XQuery library, but a word of warning — only those prefixed fn: are directly available to end users. (You'll often see XQuery users writing the fn: prefix, but it's never necessary.)
|
-- 作者:98900969r -- 发布时间:12/6/2005 1:41:00 AM -- 你忘了给出处,替你补上吧。 Learn XQuery in 10 Minutes http://www.stylusstudio.com/xquery_primer.html |
-- 作者:lonewolf -- 发布时间:12/28/2005 3:07:00 PM -- 有中文板的吗? |
-- 作者:cyclone575 -- 发布时间:2/16/2006 11:25:00 AM -- 老大,翻译一下吧,看英文好累的 |
-- 作者:SATOKO2006 -- 发布时间:2/17/2006 4:01:00 PM -- 不同的应用软件所用的Xquery的版本很可能不一样,所以现在搞Xquery是不太有明确的方向 |
-- 作者:waterstream -- 发布时间:1/28/2007 9:37:00 PM -- ok,ding up! |
-- 作者:jx -- 发布时间:3/11/2007 12:11:00 PM -- 看起来有点儿累咯。 |
-- 作者:jx -- 发布时间:4/4/2007 8:58:00 PM -- 真的不多,老半天没人回应。 |
-- 作者:braceem -- 发布时间:11/6/2007 7:55:00 PM -- 想知道研究这东西有没有前途啊。 |
-- 作者:疯狂谷 -- 发布时间:4/10/2008 11:35:00 AM -- 偶英语差,但偶尔能发现几个认识的单词! |
W 3 C h i n a ( since 2003 ) 旗 下 站 点 苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》 |
234.375ms |