Sign In/My Account | View Cart  
advertisement


Listen Print Discuss

What Is XQuery
by Per Bothner | Pages: 1, 2

Path Expressions and Relationship to XPath

XQuery borrows path expressions from XPath. XQuery can be viewed as a generalization of XPath. Except for some obscure forms (mostly unusual "axis specifiers"), all XPath expressions are also XQuery expressions. For this reason the XPath specification is also being revised by the XQuery committee, with the plan that XQuery 1.0 and XPath 2.0 will be released about the same time.

The following simple example assumes an XML file "mybook.xml" whose root element is a <book>, containing some <chapter> children:

Related Reading

XPath and XPointer
Locating Content in XML Documents
By John E. Simpson

let $book := document("mybook.xml")/book
return $book/chapter

The document function returns the root node of a document. The /book expression selects the child elements of the root that are named book, so $book gets set to the single root element.

The $book/chapter selects the child elements of the top-level book elements, which results in a sequence of the second-level chapter nodes in document order.

The next example includes a predicate:

$book//para[@class="warning"]

The double slash is a convenience syntax to select all descendants (rather than just children) of $book, selecting only <para> element nodes that have an attribute node named class whose value is "warning"

One difference to note between XPath and XQuery is that XPath expressions may return a node set, whereas the same XQuery expression will return a node sequence. For compatibility these sequences will be in document order and with duplicates removed, which makes them equivalent to sets.

XSLT is very useful for expressing very simple transformations, but more complicated stylesheets (especially anything with non-trivial logic or programming) can often be written more concisely using XQuery.

Iterating Over Sequences

A for expression lets you "loop" over the elements of a sequence:

for $x in (1 to 3) return ($x,10+$x)

The for expression first evaluates the expression following the in. Then for each value of the resulting sequence, the variable (in this case $x) is bound to the value, and the return expression evaluated using that variable binding. The value of the entire for expression is the concatenation of all values of the return expression, in order. So the example evaluates to this 6-element sequence: 1,11,2,12,3,13.

Here is a more useful example. Assume again that mybook.xml is a <book> that contains some <chapter> elements. Each <chapter> has a <title>. The following will create a simple page that lists the titles:

<html>{
  let $book := document("mybook.xml")/book
  for $ch in $book/chapter
    return <h2>{$ch/title)</h2>
}</html>

The term "FLWR expressions" includes both for and let expressions. The acronym FLWR refers to the fact that it consists of one or more for and/or let clauses, an optional where clause, and a result clause. A where clause causes the result clause to be evaluated only when the where where expression is true.

The next example has a nested loop, allowing us to combine two sequences: one of customer elements and the other of order elements. We want to find the name(s) of customers who have ordered the part whose part_id is "xx".

for $c in customers
for $o in orders
where $c.cust_id=$o.cust_id and $o.part_id="xx"
return $c.name

This is essentially a join of two tables as commonly performed using relational databases. An important goal for XQuery is that it should be usable as a query language for XML databases. Compare the corresponding SQL statement,

select customers.name
from customers, orders
where customers.cust_id=orders.cust_id
  and orders.part_id="xx"

Functions

XQuery wouldn't be much of a programming language without user-defined functions. Such function definitions appear in the query prologue of an XQuery program. It's worth noting that function parameters and function results can be primitive values, nodes, or sequences of either.

The following is a recursive utility function. It returns all the descendant nodes of the argument, including the argument node itself. It does a depth-first traversal of the argument, returning the argument, and then looping over the argument node's children, recursively calling itself for each child.

define function descendant-or-self ($x)
{
  $x,
  for $y in children($x)
    return descendant-or-self($y)
}
descendant-or-self(<a>X<b>Y</b></a>)

Which evaluates to this sequence of length 4:

<a>X<b>Y</b></a>; "X"; <b>Y</b>; "Y"

Sorting and Context

If you want to sort a sequence you can use a sortby expression. To sort a sequence of books in order of author name you can do:

$books sortby (author/name)

The sortby takes an input sequence (in this case $books) and one or more ordering expressions. During sorting the implementation needs to compare two values from the input sequence to determine which comes first. It does that by evaluating the ordering expression(s) in the context of a value from the input sequence. So the path expression author/name is evaluated many times, each time relative to a different book as the context (or current) item.

Path expressions also use and set the context. In author/name the name children that are returned are those of the context item, which is an author item.

Type Specification

XQuery is a strongly typed programming language. Like Java and C#, for example, it's a mix of static typing (type consistency checked at compile-time) and dynamic typing (run-time type tests). However, the types in XQuery are different from the classes familiar from object-oriented programming. Instead, it has types to match XQuery's data model, and it allows you to import types form XML Schema.

if ($child instance of element section)
then process-section($child)
else ( ) {--nothing--}

This invokes the process-section function if the value of $child is an element whose tag name is section. XQuery has a convenient typeswitch shorthand for matching a value against a number of types. The following converts a set of tag names to a different set.

define function convert($x) {
  typeswitch ($x)
    case element para return <p>{process-children($x)}</p>
    case element emph  return <em>{process-children($x)}</em>
    default return process-children($x)
}
define function process-children($x) {
  for $ch in children($x) return convert($ch)
}

Resources

The primary XQuery resource is www.w3.org/XML/Query. This has links to the draft standards, mailing lists, and implementations. The main documents are

There's only one XQuery book so far, mainly because there are significant loose ends in the specification: Early Adopter XQuery from Wrox. I am co-authoring (with James McGovern, Kurt Cagle, James Linn and Vaidyanathan Nagarjan) XQuery Kick Start for Sams Publishing, due to be released in 2003. There are no complete standards-conforming implementations either, but the XQuery site lists known implementations, some of which have executable demos. The only open-source implementation currently available seems to be my Qexo. (The Qexo implementation is interesting in that it compiles XQuery programs on-the-fly directly to Java bytecodes.) I recommend considering XQuery when you need a powerful and convenient tool for analyzing or generating XML.


Comment on this articleGot a question on XQuery? Ask it in our forum.
(* You must be a
member of XML.com to use this feature.)
Comment on this Article


Titles Only Titles Only Newest First
  • Locksmith Los Angeles 877-364-5264 Locksmith Security Services, Los Angeles Locks, Safe & Vault Sales and Installation.
    2008-12-21 12:47:41 services123 [Reply]

    A. Locksmith Los Angeles 877-364-5264 Locksmith Security Services, Los Angeles Locks, Safe & Vault Sales and Installation.
    For All Your Home & Business Needs Doors installation wood, metal, Kalamein, and fire rated wood doors for both heavyweight commercial and standard residential needs. Door Closers Panic Bars Fire Exit Devices Floor or Head Check Bolts Door Frames Hinges Buzzer Systems Card Access Elevator Doors magnetic door locks, electric strikes, CCTV install Dvr Video Camera master key systems, CCTV and video surveillance, Alarm Systems, Keyless Entry Systems and Access Control, Iron Works specializes in custom exterior and interior wrought iron design. We design and hand forge wrought iron entrance gates, driveway gates Child guard gates A/C cage gates Elevator doors Backyard gates Fire exit gates Custom design gates Storefront and Commercial Rolling Access Control Automotive Lockout Tools & Kits CodeLock Pushbutton Locks Cylinders Deadbolt Style Gate Locks DigiLock Digital Locks Door Closers Door Locks Electromagnetic Locks Electronic Hardware Exit Hardware Four Drawer Key File Gate Locks & Weldable Gate Box Government Locks High Security Locks Hospital Locks IEI Digital Pushbutton Locks Key Blanks Key Cabinets & Key Storage Key Cutters Key Tags and Key Rings, Key Towers Knob and Lever Gate Locks Locker Locks Locker, Cabinet & Furn. Locks Locksmith Tools and Equipment Manufacture Line Items Misc Hardware Misc Locking Devices NEW PRODUCTS Omni Lock Pepper Spray Picks and Pick Sets Pins and Pinning Kits Pushbutton Gate Locks Pushbutton Locks Re-Keying Safes Schlage Simplex Parts Simplex Pushbutton Lock Simplex, CodeLock, Trilogy Specials Training and Education Trilogy Digital locks Von Duprin Window Locks Patio Door Locks Padlocks.
    Locksmith in Los Angeles (877) 364-5264

  • Carpet Cleaners los angeles 1-818-386-1022
    2008-09-28 15:45:31 0 [Reply]

    Carpet Cleaners los angeles 1-818-386-1022
    Los Angeles Carpet Cleaning services is the place for carpet cleaning, upholstery cleaning and air duct cleaning needs. Come see the experts at carpet cleaning, Spot and Stain Removal Pet Stain and Odor Removal, upholstery cleaning, tile and grout cleaning and stain removal. Learn about our two-step process and satisfaction guarantee. We have expert carpet cleaners, upholstery cleaners and air duct cleaners standing by! call 1-818-386-1022


  • IE6/Win + XQuery
    2006-10-27 09:08:08 frederick.c.lee [Reply]

    Greetings from a neophyte:
    I'm stuck with using Internet Explorer version 6 on Windoz (which only supports Javascript 1.3) for a client.


    I can't do XML recursion as I can with Firefox 1.5+ and other W3C-compliant browsers.



    XQuery has gotten my attention here.


    1) Does IE6/Win support XQuery?


    2) How do I implement it on the client-side IE6? I want to parse a DOM data item that I get from the back-end PHP via XAJAX.


    Regards,


    Ric.

  • XQuery Search
    2004-12-21 11:40:44 dessym79 [Reply]

    I have created an xml page that list documents and links to the documents. I have used an xslt page to display the list and links in html. I would like to incorporate a search at the top that would read what the user enters and show only those documents that match the search. Will XQuery do this for me? If so could I get a sample code? If not what program should I use?

    • XQuery Search
      2004-12-22 01:14:59 Per Bothner [Reply]

      "Will XQuery do [a search] for me?"


      Probably - you should be able to search and XML document and select only those elements that satisfy some predicate, such as matching a regexp.


      "If so could I get a sample code?"


      I'm sorry, but I don't have enough time to write my own programs, so you can't expect me to write yours.

  • I need to create my own Mark up language
    2004-04-08 09:42:30 Senthil Jeya prakash [Reply]

    Hi,
    I'm planning to create my own mark up language like MathML,etc...


    please guide my how to goahead with that.i have some concepts and ideas,but i dont know the standards to follow.


  • Please help
    2004-04-08 09:40:06 Senthil Jeya prakash [Reply]

    I'm new to XQuery,please let me know how to save a XQuery file and how to execute it.


    My objective it get some information from an XML file,let me know exactly how where to put this
    doc("books.xml") query.


    should i put it in a XML or HTML file??

    • Please help
      2004-04-09 14:39:23 Per Bothner [Reply]

      "please let me know how to save a XQuery file and how to execute it."


      This article gives you practical information about using Qexo implementation of XQuery:
      http://www.gnu.org/software/qexo/Running.html


      "should i put it in a XML or HTML file??"


      No, an XQuery "program" is a stand-alone text file. Write it using any programmer's text editor.

      • Please help
        2006-12-19 23:36:08 success123 [Reply]

        use of xquery in marklogic

        • Please help
          2006-12-20 13:53:20 Per Bothner [Reply]

          Sorry, I know nothing about XQuery in MarkLogic. I suggest checking MarkLogic's documentation or support.

  • children()?
    2004-03-03 19:18:14 Jun Wang [Reply]

    Hi,
    I think children() is never a standard function although you wrote so in your article.
    Thanks.

    • children()?
      2004-03-03 19:39:54 Per Bothner [Reply]

      "I think children() is never a standard function although you wrote so in your article."


      You're quite right. But remember this article is quite old. Both the XQuery language - and my understanding of it - have changed quite a bit. An update would be desirable, when/if I have time.


      Specifically, children is a "data model" function used to describe the semantics of XQuery, but it isn't a user-accessible function.

  • idref
    2003-11-11 17:59:21 Jyothi ratna [Reply]

    Hello,
    I am a beginner in Xquery.In my schema I have element which has IDREF attribute.I want to extract id value to which points and check whether that value is equivalent to some value.
    eg:<AUTHOR EMAILID ="psmith@sfsu.edu"> >
    <FNAME>smith</FNAME>
    <MI>j</MI>
    <LNAME>jen</LNAME>
    </AUTHOR>
    <PAPER PAPERID = 10
    EMAILID = "sai@yahoo.com"
    >
    <MAINATHR>SHARMA</MAINATHR>
    <TITLE>ABC</TITLE>
    </PAPER>
    <PAPER PAPERID = 10
    EMAILID = "sha@yahoo.com"
    >
    <MAINATHR>SHARMA</MAINATHR>
    <TITLE>ABC</TITLE>
    </PAPER>


    Here EMAILID is a IDREF attribute in PAPER element.I want to check the value of EMAILID of paper element and check if it is "sai@yahoo.com ".If it is "sai@yahoo.com" I have to retrieve paperid,author fname from the document.I think I may have to use dereference operator => but I do not know how to apply that in my situation.Could anybody please help me to solve this problem?
    Thanks,
    Sharma

    • idref
      2003-11-12 11:32:24 Per Bothner [Reply]

      This should get you on the right track. Note that for more complex conditions you might want to use a 'where' clause rather than a [predicate].


      let $id := "sai@yahoo.com",
      for $paper in $doc/PAPER[@EMAILID = $id],
      for $author in $doc/AUTHOR[@EMAILID = $id]
      return
      <result PAPERID="{$paper/@PAPERID}">
      {$author/FNAME}
      <result>


      This assumes you have a specific emailid. If you want this for all emailids, you could do:


      for $paper in $doc/PAPER,
      for $author in $doc/AUTHOR
      where $paper/@EMAILID = $auther/$EMAILID
      return
      <result PAPERID="{$paper/@PAPERID}">
      {$author/FNAME}
      <result>


      XQuery does not have the => operator.

  • operations on xml
    2003-07-19 21:43:28 Johnny Wang [Reply]

    Hi, I'm new to XQaury, someone told me it is the right tool to solve my problem, still don't yet see how to do it. Any pointers are appreciated.


    Given an xml document, I'd like to make some changes to the content (e.g., change an attribute, or switching the order of two child nodes, or add a new node). I can do all these using standard programming languages and DOM (e.g., java and JDOM), but I found these really repetative and intellecturally wanting. I'd like to just give an instruction in some language, and a general transformer should do the actual work. Is XQuery the tool for this type of task?


    Thanks.


    • operations on xml
      2003-07-27 10:48:25 Per Bothner [Reply]

      > Given an xml document, I'd like to make some changes to the content ...


      You can certainly do that, as long as you realize "changes" means creating a modified version. XQuery does not have side-effects, and you cannot update (modify) existing nodes. (Though there are proposals to extend XQuery with update operations, they will not be considered for XQuery 1.0.) So you have to "write out" a new modified version of the document, rather than update it in place.

  • Please Help
    2003-07-09 00:59:29 Bless george [Reply]

    I am new to XML. please guide me how to start off with the same. i have some experience in HTML. i hope that i'll get some valuable help from you all. basically i want to learn how to connect to databases.


    with warm regards


    blesson

  • Newbie to XQuery
    2003-03-23 10:17:25 Mehran Zonouzi [Reply]

    Hi,


    I am completely new to XQuery..
    I have the following which I would like to run on an .xml file...


    for $book in bib.xml//book
    let $title := $book/title
    where $book/publisher = 'Addison-Wesley'
    return

    <bookInfo>
    { $title }

    </bookInfo>


    and the bib.xml file
    <bib>
    <book year="1994">
    <title>TCP/IP Illustrated</title>
    <author><last>Stevens</last><first>W.</first></author>
    <publisher>Addison-Wesley</publisher>
    <price> 65.95</price>
    </book>

    <book year="1992">
    <title>Advanced Programming in the Unix environment</title>
    <author><last>Stevens</last><first>W.</first></author>
    <publisher>Addison-Wesley</publisher>
    <price>65.95</price>
    </book>

    <book year="2000">
    <title>Data on the Web</title>
    <author><last>Abiteboul</last><first>Serge</first></author>
    <author><last>Buneman</last><first>Peter</first></author>
    <author><last>Suciu</last><first>Dan</first></author>
    <publisher>Morgan Kaufmann Publishers</publisher>
    <price>39.95</price>
    </book>

    <book year="1999">
    <title>The Economics of Technology and Content for Digital TV</title>
    <editor>
    <last>Gerbarg</last><first>Darcy</first>
    <affiliation>CITI</affiliation>
    </editor>
    <publisher>Kluwer Academic Publishers</publisher>
    <price>129.95</price>
    </book>

    </bib>


    What exactly do I have to do to run this query?





    • Newbie to XQuery
      2003-03-23 13:10:35 Per Bothner [Reply]

      Try this URL for information on using the Qexo implementation of XQuery:
      http://www.gnu.org/software/qexo/Running.html

  • Performance on large datasets?
    2002-12-18 21:55:28 Larry Leeth [Reply]

    I'm investigating the feasibility of using XML documents for long-term archival of DB records.


    In addition to storing the data, a mechanism must be provided to perform searches/queries on the XML generating reports.


    Over a period of months/years, the volume of XML data could exceed 100's of millions or records and many gigabytes of data.


    Since there is no equivalent of a DB index to limit the number of records that must be processed, what are the performance parameters of XML queries/searches?


    Is this a reasonable choice - given that queries/searches will need to be supported?


    Larry Leeth
    lal@qad.com


  • Another open-source implementation: Galax
    2002-10-18 09:14:15 Per Bothner [Reply]

    A couple of people have pointed out that there is at least another active open-source XQuery implementation project:
    Galax (http://db.bell-labs.com/galax/) which is "a lightweight, very portable implementation, with a liberal open source license".


    Galax is implemented in O-Caml, while Qexo is implemented in Java. In terms of feature coverage, Galax appears to be further along (though both are useful as is); on the other hand, Qexo is a true compiler.

  • Very Interesting
    2002-10-18 06:31:58 Ivelin Ivanov [Reply]


    So, what does the future hold for XSLT?
    XQuery started as a replacement for SQL and it became a fully functional language.
    I like the fact the just like with XSLT it is a pure functional language (no side effects) which will allow extreme optimization to be applied.


    Why is the W3C body allowing 2 directly competing standards to emerge together?


    So far all XML standards have been complimenting each other, not competing.


    Are there XQuery examples for bigger programs which require packaging, organizing different files, etc.


    Very good article


    Ivelin

    • Future of XSLT? [was: Very Interesting]
      2002-10-18 09:00:35 Per Bothner [Reply]

      > Why is the W3C body allowing 2 directly competing standards to emerge together?


      I guess because there is a need for XQuery, as it is usable for applications where XSLT is very unsuitable, and XSLT is already well-established. In any case, they use two very different approaches, and there is nothing wrong with allowing multiple alternatives.


      > Are there XQuery examples for bigger programs which require packaging, organizing different files, etc.


      No, this kind of modularization is a weak area in the current drafts = and a strength of XSLT.


      > Very good article


      Thanks! At least one more (which emphasizes the XSLT target area of generating XML/HTML) will appear on xml.com in the near future.


      • Future of XSLT? [was: Very Interesting]
        2002-10-21 23:37:24 Brandon Ibach [Reply]

        > I guess because there is a need for XQuery, as
        > it is usable for applications where XSLT is
        > very unsuitable


        An intriguing statement... Examples?


        • Future of XSLT? [was: Very Interesting]
          2002-10-24 11:17:25 Per Bothner [Reply]

          XSLT is designed for reading a single input file and generating a single output file. There are extensions for multiple input and output files, but the basic model is one-to-one. If you need to merge multiple documents, for example, then XSLT is difficult.


          Furthmore, XSLT is very awkward for any kind of real programming. Defining and calling functions is very awkard and tedious. The syntax is very verbose, hence the escape into the much more compact XPath. One might say that XQuery is XPath extended into a full programming language.

          • Future of XSLT? [was: Very Interesting]
            2003-01-06 17:37:12 Brandon Ibach [Reply]

            > XSLT is designed for reading a single input file
            > and generating a single output file. There are
            > extensions for multiple input and output files,
            > but the basic model is one-to-one. If you need
            > to merge multiple documents, for example, then
            > XSLT is difficult.


            I've not found it difficult. The document() function has existed in XSLT all along, and you seem to use it in your XQuery examples, as well. How is XQuery so different in this regard?


            As for multiple output documents, it appears (from what I've seen in your articles, I'm not well versed on the current state of the standard) that XQuery has no standardized method. XSLT 1.0 didn't, either, although most implementations ended up providing a similar mechanism, which is being standardized in XSLT 2.0.
            > Furthmore, XSLT is very awkward for any kind of
            > real programming. Defining and calling
            > functions is very awkard and tedious. The
            > syntax is very verbose, hence the escape into
            > the much more compact XPath. One might say that
            > XQuery is XPath extended into a full programming
            > language.


            Granted, XSLT is rather verbose, but that is the nature of XML. SGML had a number of (IMHO) slick methods for cutting down on markup characters, but these were discarded in the design of XML, for simplicity.


            I think XPath in XSLT is a nice compromise between compactness and rich structure. However, XQuery has essentially dove to one end, abandoning XML-based structure entirely just for compactness. XSLT is getting some improvements in this area with XSLT 2.0, in that it allows for defining new XPath functions using XSLT and XPath, in a standardized way (again, similar to proprietary implementations that have existed for a while).


            In the end, I fail to see what XQuery provides, other than a less structured, more compact syntax for the things I do now in XSLT, but minus the built-in template processing model, and with no apparent basis for the kind of extensibility that XSLT has allowed.


            Please don't think I'm just here to flame XQuery. I really am trying to evaluate the technology, and understand the motives and intents behind its design.

            • Future of XSLT? [was: Very Interesting]
              2003-01-22 22:44:27 Per Bothner [Reply]

              >> If you need yo merge multiple documents,
              >> for example, then XSLT is difficult.


              > I've not found it difficult. The document()
              > function has existed in XSLT all along, and you
              > seem to use it in your XQuery examples, as well.


              However you cannot pass the result from document
              through the XSLT template processor. You can
              insert the result in the output - anything else
              is quite tedious.


              > How is XQuery so different in this regard?


              The difference is that that XQuery allows
              element construction, document input, and
              node selection/extraction to be combined
              and nested much more flexibly.


              > However, XQuery has essentially dove to one end, > abandoning XML-based structure entirely just for > compactness.


              Compactness is an important value in a programming
              language. Programming errors tend to be
              proportional to program length, and productivity
              as measured in tokens per programmer-day seems
              to be relatively language-independent. So
              compactness is valuable. (My impression is
              that number of tokens is more important than
              number of characters, so perhaps APL and Perl
              go too far. Note this is just what I vaguely
              remember reading - I don't have references.)


              While XML is a good syntax for documents and
              data, it is (I think) a terrible syntax for a
              programming language.

              • Future of XSLT? [was: Very Interesting]
                2003-02-21 12:43:32 Brandon Ibach [Reply]

                > However you cannot pass the result from document
                > through the XSLT template processor. You can
                > insert the result in the output - anything else
                > is quite tedious.


                Actually, I use template rules to process nodes from other documents regularly, and I'm not aware of anything in the XPath 1.0 or XSLT 1.0 specs that prevents this. Please correct me if I'm mistaken on this.


                > The difference is that that XQuery allows
                > element construction, document input, and
                > node selection/extraction to be combined
                > and nested much more flexibly.


                This is a very vague statement. *How* does XQuery allow these things to be combined more flexibly? Again, examples of use cases (or pointers to them) where XQuery provides a significantly easier or more efficient solution than XSLT would clarify your point.


                > Compactness is an important value in a
                > programming language. Programming errors tend
                > to be proportional to program length, and
                > productivity as measured in tokens per
                > programmer-day seems to be relatively
                > language-independent.
                > While XML is a good syntax for documents and
                > data, it is (I think) a terrible syntax for a
                > programming language.


                Agreed. Every attempt to create a programming language based on XML syntax has either been too awkward to use or has invented some "alterations" to XML to make it more usuable, such that it really isn't XML anymore. In a way, XSLT falls into the latter category. A good bit of the logic in XSLT is expressed in XPath, which uses a non-XML syntax. As seen in the XPath 2.0 and XSLT 2.0 drafts, even more power is being put into XPath. As I've said, I think there's a good balance there, so it satisfies both the compact-syntax-desiring programmer in me and the markup fanatic in me. Given the almost complete abandonment of XML syntax in XQuery, I'm not sure how different it really is from my favorite scripting language with a good XPath library.


                One final question. In comparing XSLT and XQuery, are you keeping in mind the improvements that will be coming with XSLT 2.0? Given that XQuery uses XPath 2.0, and thus benefits from lessons learned with XPath 1.0 and XSLT 1.0, it would hardly be fair to compare to the older standards.


                [ Side note: Does xml.com have some way to get notified when someone replies to your post? This discussion is getting drawn out quite a bit as I keep forgetting to check back for responses. ]

                • Future of XSLT? [was: Very Interesting]
                  2003-02-21 17:30:47 Per Bothner [Reply]

                  > Actually, I use template rules to process nodes from other documents regularly, and I'm not aware of
                  > anything in the XPath 1.0 or XSLT 1.0 specs that prevents this.


                  Perhaps we're talking about different things. Could you give an example of how you do this?


                  > This is a very vague statement. *How* does XQuery allow these things to be combined more flexibly?


                  XQuery allows you to nest element constructor expressions (XML syntax) inside complex XQuery expressions, and you can conversely nest complex
                  XQuery expressions inside element constructors.
                  With XSLT, you can nest XPath expressions inside
                  XML, but you can't nest XML constructors inside
                  XPath expressions. So a loop or recursive
                  function that returns new XML nodes is difficult
                  to write.


                  > Given the almost complete abandonment of XML syntax in XQuery, I'm not sure how different it
                  > really is from my favorite scripting language with a good XPath library.


                  The big difference is how easy it is to *create*
                  XML nodes using XQuery, and then process them
                  just like nodes from an input document.
                  With e.g. JSP you create *text*, not *nodes*.
                  See my later article:
                  http://www.xml.com/pub/a/2002/12/23/xquery.html


                  > One final question. In comparing XSLT and XQuery, are you keeping in mind the improvements
                  > that will be coming with XSLT 2.0?


                  I'm trying to, though I don't have as good a picture of XSLT 2.0.


                  > Side note: Does xml.com have some way to get notified when someone replies to your post? This
                  > discussion is getting drawn out quite a bit as I keep forgetting to check back for responses.


                  I know I as an author get notification, but I
                  don't know if you can request it.


                  • Future of XSLT? [was: Very Interesting]
                    2003-03-06 06:35:10 Brandon Ibach [Reply]

                    > > Actually, I use template rules to process nodes from other documents regularly, and I'm not aware of
                    > > anything in the XPath 1.0 or XSLT 1.0 specs that prevents this.
                    > Perhaps we're talking about different things. Could you give an example of how you do this?


                    Certainly. :)


                    <xsl:template match="order">
                    <!-- Generate invoice header with customer's name, address, etc. -->
                    <xsl:for-each select="item">
                    <xsl:apply-templates select="document('products.xml')//product[@id = current()/@ref]">
                    <xsl:with-param name="item" select="."/>
                    </xsl:apply-templates>
                    </xsl:for-each>
                    </xsl:template>


                    <xsl:template match="product">
                    <xsl:param name="item"/>
                    <!-- Generate invoice line-item for this product, using information from both the current node (product), as well as the item node from the order document -->
                    </xsl:template>


                    This, of course, is just one way in which this sort of thing can be done. Is this what you had in mind?


                    > > This is a very vague statement. *How* does XQuery allow these things to be combined more flexibly?


                    > XQuery allows you to nest element constructor expressions (XML syntax) inside complex XQuery expressions, and you can conversely nest complex
                    > XQuery expressions inside element constructors.
                    > With XSLT, you can nest XPath expressions inside
                    > XML, but you can't nest XML constructors inside
                    > XPath expressions. So a loop or recursive
                    > function that returns new XML nodes is difficult
                    > to write.


                    It is possible to set a variable to a result tree fragment, then use that variable in another XPath expression, though you're right that it can't be done directly, and RTFs are a pain to work with, anyway, due to the restrictions on them.
                    However, RTFs go away with XSLT 2, and everything is a node set, so generating XML nodes that can be used just as if they came from an input document will be easier. Also, the addition of standard support for XPath extension functions defined in XSLT will provide another flexible way to do this.
                    As for loops and recursive functions, I create these regularly using <xsl:call-template>. The syntax does make it a little awkward, but not unreasonable, and this will also get easier with the new extension function support in XSLT 2.


                    > > Given the almost complete abandonment of XML syntax in XQuery, I'm not sure how different it
                    > > really is from my favorite scripting language with a good XPath library.


                    > The big difference is how easy it is to *create*
                    > XML nodes using XQuery, and then process them
                    > just like nodes from an input document.
                    > With e.g. JSP you create *text*, not *nodes*.


                    I suppose I can see that. Offhand, I can't think of an example of where this would be important to me, but maybe I just don't have the right kind of application in mind. *shrug*


                    > > One final question. In comparing XSLT and XQuery, are you keeping in mind the improvements
                    > > that will be coming with XSLT 2.0?


                    > I'm trying to, though I don't have as good a picture of XSLT 2.0.


                    The current draft spec seems to outline the changes fairly well. I think the most important aspects, at least for this discussion, are the improvements in XPath 2.0, with which you must be quite familiar, the elimination of result tree fragments and the standardization of XPath extension functions written in XSLT.


                    I'm still eager to see some examples that show how XQuery can solve the same problem better than XSLT. Can you point me to something like this? I really am interested in the possibility of applying XQuery in my own work, but haven't found the compelling reason to use it over XSLT.


                    • Future of XSLT? [was: Very Interesting]
                      2003-03-15 23:15:19 Per Bothner [Reply]

                      > <xsl:apply-templates select="document('products.xml')//product[@id = current()/@ref]">


                      The XSLT 2.0 draft spec 6.2 says "The xsl:apply-templates instruction takes as input a sequence of nodes in the source tree". One could argue that prohibits your usage, since the nodes aren't in the "source tree". But I think it's
                      just a poor choice - I agree it *should* be allowed.


                      The main advantage of XQuery is that everything
                      nests. You can iterate over one of more
                      data-sets (i.e. a join), construct elements
                      containg arbirary expressions based on each
                      value of the "loop", and pass arbitary data
                      as arguments and results of user-defined functions. I don't offhand know if there is
                      a set of XQuery programs you can't express
                      in XSLT, but think many of them will be a lot
                      more natural and compact using XQuery.


                      For now we'll just have to disagree, at least
                      until there is more experience with XQuery, or
                      I read a good analysis of their relative "power".


                      >The big difference is how easy it is to *create*
                      >> XML nodes using XQuery, and then process them
                      >> just like nodes from an input document.
                      >> With e.g. JSP you create *text*, not *nodes*.


                      > I suppose I can see that. Offhand, I can't think of an example of where this would be important to
                      > me, but maybe I just don't have the right kind of application in mind.


                      Think of filters, where you're passing data from
                      one transformation to another. If the data are
                      nodes rather than text, not only can you express
                      a transformation chain very compactly and
                      naturally as a set of function calls, but it
                      also should be more efficient than continuously
                      formatting and parsing the data.


                      > I'm still eager to see some examples that show how XQuery can solve the same problem better than
                      > XSLT. Can you point me to something like this? I really am interested in the possibility of
                      > applying XQuery in my own work, but haven't found the compelling reason to use it over XSLT.


                      Well, perhaps for you, who are fluent in XSLT,it's not that compelling. But I'd be curious to see
                      how you'd the photo album application. See
                      http://xml.com/pub/a/2002/12/23/xquery.html
                      - the latest version version of the XQuery source is
                      http://www.gnu.org/software/qexo/qalbum/pictures.xql
                      and an example showing most of the features is
                      http://pics.bothner.com/2002/BristleconePines/ .)

  • nested loop
    2002-10-17 03:30:05 Ian Young [Reply]

    "The next example has a nested loop"
    No. The example has a nested for clause. Whether the implementation executes this as a nested loop is the choice of the implementation.


    • nested loop
      2002-10-18 09:07:12 Per Bothner [Reply]

      > No. The example has a nested for clause. Whether the implementation executes this as a nested loop is the choice of the implementation.


      Well, if you want to be pedantic: A C compiler has the same choice(s)!


      You're right, but in a brief overview the phrase "nested loop" seems a nice compact way of getting the idea across. Most people are most comfortable with operational semantics anyway.

      • nested loop
        2002-11-01 05:16:18 Ian Young [Reply]

        > A C compiler has the same choice


        I was thinking more in terms of SQL joins.
        To produce an execution plan for a SQL query, the compiler has to choose in what order the joins will be performed, and what join strategy to use for each (nested loops, loop + index seek, hash table, merge, etc.)