Questions tagged [xquery]

XQuery is a functional language designed to query and manipulate XML data. It is a superset of XPath, to which it adds features such as the creation of new nodes and more powerful FLWOR expressions. Although it shares its data model with XSLT, XQuery is optimized for querying rather than transforming data, and as such it has a different design inspired by SQL.

W3C XML Query

XQuery has a rich set of features that allow many different types of operations on data and documents, including:

  • Selecting information based on specific criteria
  • Filtering out unwanted information
  • Updates
  • Searching for information within a document or set of documents
  • Joining data from multiple documents or collections of documents
  • Sorting, grouping, and aggregating data
  • Transforming and restructuring XML data into another XML vocabulary
    or structure
  • Performing arithmetic calculations on numbers and dates
  • Manipulating strings to reformat text

XQuery can be used not just to extract sections of XML documents, but also to manipulate and transform the results.

Modified from O'Reilly XQUERY ISBN-10: 0-596-00634-9 By Priscilla Walmsley.

Relationship to XSLT

XQuery has a large overlap with in terms of purpose but has a syntax and model inspired largely by . Most built in functions are shared with XSLT.

Relationship to XPath

XQuery is mostly a superset of . XQuery version 1 is based on XPath version 2. XQuery version 3 is based on XPath version 3. Version 2 was skipped in order to align the XQuery and XPath version numbers. XQuery 3.1 like XPath 3.1 supports JSON processing through the new map and array types and JSON <--> XML conversion using the xml-to-json and json-to-xml functions as well as parse-json and json-doc functions.

Implementations

  • A list of the current implementations according to the W3C XML Query group.
  • Version 9.8 and later of the Saxon processor support both XSLT 3.0 and XQuery 3.1.
  • BaseX implements all versions of the language and can be use through a CLI.

References

Online training

Books

5333 questions
22
votes
4 answers

XQuery library under Python

Is there any existing way to run XQuery under python? (not starting to build a parser yourself in other words). I got a ton of legacy XQuery that I want to port to our new system, or rather I want to port the framework and not XQuery. Therefore: Is…
Ooki
  • 351
  • 1
  • 2
  • 6
21
votes
4 answers

How to concatenate all strings from a certain column for each group

Suppose I have this table [Table1] Name Mark ------- ------ ABC 10 DEF 10 GHI 10 JKL 20 MNO 20 PQR 30 What should be my SQL statement to retrieve a record that looks like this: (group by [mark]). I have done the 1 and 2…
yonan2236
  • 13,371
  • 33
  • 95
  • 141
20
votes
1 answer

Using XQuery/XPath to get the attribute value of an element's parent node

Given this xml document: this is the first baz
Mark Stewart
  • 203
  • 1
  • 2
  • 4
19
votes
3 answers

XQuery vs XSLT for transforming data

What do you use in your applications for transforming XML data to other data types? WHY?
metdos
  • 13,411
  • 17
  • 77
  • 120
19
votes
6 answers

Finding node order in XML document in SQL Server

How can I find the order of nodes in an XML document? What I have is a document like this:
erikkallen
  • 33,800
  • 13
  • 85
  • 120
18
votes
1 answer

how to use if else in xquery assignment

I am trying to use a if condition to assign a value to a variable in an xquery. I am not sure how to do this. This is what I tried: declare namespace libx='http://libx.org/xml/libx2'; declare namespace atom='http://www.w3.org/2005/Atom'; declare…
sony
  • 1,557
  • 10
  • 36
  • 50
17
votes
3 answers

Debug XQuery Program

In the beginning, I wrote a little XQuery script that was designed to do a small task and only that. Now it came out that this XQuery script is anything but complete, and I have to fix it. The problem is, my XQuery evolved into a program, and XQuery…
Arne
  • 7,921
  • 9
  • 48
  • 66
16
votes
7 answers

How can I remove duplicate nodes in XQuery?

I have an XML document I generate on the fly, and I need a function to eliminate any duplicate nodes from it. My function looks like: declare function local:start2() { let $data := local:scan_books() return {$data} }; Sample…
brabster
  • 42,504
  • 27
  • 146
  • 186
16
votes
2 answers

XQuery order by ascending and descending

In XQuery, How do you order by ascending and descending? I have got the following from a tutorial: for $x in doc("books.xml")/bookstore/book where $x/price>30 order by $x/title return $x/title would it be for $x in…
Tuffy G
  • 1,521
  • 8
  • 31
  • 42
15
votes
4 answers

Put each XQuery result on a new line

Is it possible to put each XQuery result on a new line? I thought I remembered there was an attribute to set at the beginning of the document, but can't remember. :/ I have this .xq file: (: declare default element namespace…
Chris V.
  • 1,123
  • 5
  • 22
  • 50
15
votes
3 answers

XPath pulling more than one match

The (BaseX) error I am running queries on a large dataset in BaseX, but one XQuery is crashing my programme with an error [XPTY0004] Item expected, sequence found: (attribute begin {"6"}, ...).. In my query I am trying to make sure that one element…
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
15
votes
1 answer

Using XQuery in Linq To SQL?

Let's say I have a table that has a column of XML type data. Within SQL, I can execute the following statement: select top 10 *, Content.value('(/root/item/value)[1]', 'float') as Value from xmltabletest where …
Curtis Batt
  • 464
  • 4
  • 14
15
votes
2 answers

How to get the ROOT node name from SQL Server

I have a table where ID is integer and XML is XML data type. ID XML ---------------------- 1 ... 2 ... 3 ... 4 ... How do I get the result below? ID XML ------------- 1 …
KS Kian Seng
  • 319
  • 1
  • 6
  • 14
14
votes
3 answers

XQuery all files under a specific directory?

Can I use XQuery to query all XML files under a specific directory? All the XML files have the same structure. Also, from what I have seen you can XQuery many files but you need to write the names of them in the query. In my case, I need to query…
Nick J.
  • 143
  • 1
  • 4
13
votes
5 answers

How do you switch on a string in XQuery?

I have an external variable coming in as a string and I would like to do a switch/case on it. How do I do that in xquery?
Sixty4Bit
  • 12,852
  • 13
  • 48
  • 62