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
6
votes
1 answer

XQuery - How to get 90 days before date from a given date?

In xquery, How to get 90 days before date from a given date? Say given date is 30-03-2012. I want to get the date which is 90 days before the given date. Leap year calculation should also not get missed. I could not find any built in functions.…
Radhika
  • 423
  • 7
  • 22
6
votes
1 answer

XQuery/XPath uses sql parameter?

I am try to implement the following code. declare @para varchar(10) = 'b'; declare @x xml = ' 1111 2222 3333 '; select @x.query('/x/sql:variable("@para")'); The above code should get the node of 2222.…
ca9163d9
  • 27,283
  • 64
  • 210
  • 413
6
votes
1 answer

Insertion of an data in an XML using Basex

I am storing two XML documents, namely hospital and office, in BaseX. The following is the office xml: 666-66-6666
Sadesh Kumar N
  • 1,962
  • 7
  • 19
  • 26
6
votes
5 answers

Grouping and counting in Xquery

Hear is XML. I am trying to get Number of titles published by an author in a date range 15/02/2012 to 24/02/2012 order by highest to lowest (number of titles). 1 23/02/2012 Title…</div> <div class="grid ai-start jc-space-between fw-wrap"> <div class="grid gs4 fw-wrap tags "> <a href="../../questions/tagged/xquery" class="post-tag grid--cell" title="show questions tagged 'xquery'" rel="tag">xquery</a> <a href="../../questions/tagged/marklogic" class="post-tag grid--cell" title="show questions tagged 'marklogic'" rel="tag">marklogic</a> </div> <div class="started mt0"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="asked Feb 22 '12 at 19:01">asked Feb 22 '12 at 19:01</time> <a href="../../users/637711/pankaj" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/637711.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Pankaj" /> </a> <div class="s-user-card--info"> <a href="../../users/637711/pankaj" class="s-user-card--link">Pankaj</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">177</li> <li class="s-award-bling s-award-bling__silver" title="1 silver badges">1</li> <li class="s-award-bling s-award-bling__bronze" title="9 bronze badges">9</li> </ul> </div> </div> </div> </div> </div> </div> </div> <div class="mln24"> <div class="question-summary" id="question-summary-8607464"> <div class="statscontainer"> <div class="stats"> <div class="vote"> <div class="votes"> <span class="vote-count-post"><strong>6</strong></span> <div class="viewcount">votes</div> </div> </div> <div class="status answered-accepted"> <strong>2</strong> answers </div> </div> </div> <div class="summary"> <h3><a href="../../questions/8607464/how-to-count-each-instance-of-distinct-values-in-xquery" class="question-hyperlink">How to count each instance of distinct-values in XQuery?</a></h3> <div class="excerpt">I have an XML file with the following elements and attributes: <element> <unit id="1" color="blue"/> <unit id="2" color="blue"/> <unit id="3" color="red"/> </element> How do I get the count of 1) distinct occurrences of each attribute color and 2)…</div> <div class="grid ai-start jc-space-between fw-wrap"> <div class="grid gs4 fw-wrap tags "> <a href="../../questions/tagged/xml" class="post-tag grid--cell" title="show questions tagged 'xml'" rel="tag">xml</a> <a href="../../questions/tagged/count" class="post-tag grid--cell" title="show questions tagged 'count'" rel="tag">count</a> <a href="../../questions/tagged/xquery" class="post-tag grid--cell" title="show questions tagged 'xquery'" rel="tag">xquery</a> <a href="../../questions/tagged/distinct-values" class="post-tag grid--cell" title="show questions tagged 'distinct-values'" rel="tag">distinct-values</a> </div> <div class="started mt0"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="asked Dec 22 '11 at 17:12">asked Dec 22 '11 at 17:12</time> <a href="../../users/1108782/ritzdiamond" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/1108782.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="ritzdiamond" /> </a> <div class="s-user-card--info"> <a href="../../users/1108782/ritzdiamond" class="s-user-card--link">ritzdiamond</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">269</li> <li class="s-award-bling s-award-bling__gold" title="1 gold badge">1</li> <li class="s-award-bling s-award-bling__silver" title="2 silver badge">2</li> <li class="s-award-bling s-award-bling__bronze" title="17 bronze badge">17</li> </ul> </div> </div> </div> </div> </div> </div> </div> <div class="mln24"> <div class="question-summary" id="question-summary-8468003"> <div class="statscontainer"> <div class="stats"> <div class="vote"> <div class="votes"> <span class="vote-count-post"><strong>6</strong></span> <div class="viewcount">votes</div> </div> </div> <div class="status answered-accepted"> <strong>3</strong> answers </div> </div> </div> <div class="summary"> <h3><a href="../../questions/8468003/using-xquery-flwor-expressions-to-find-multiple-where-restrictions" class="question-hyperlink">Using xquery FLWOR expressions to find multiple "where" restrictions</a></h3> <div class="excerpt">I am trying to find employees who work on projects located in Houston but the department the project is housed in is not located in Houston. I was trying to model the expression after this example of FLWOR expressions but the query doesn't return…</div> <div class="grid ai-start jc-space-between fw-wrap"> <div class="grid gs4 fw-wrap tags "> <a href="../../questions/tagged/xml" class="post-tag grid--cell" title="show questions tagged 'xml'" rel="tag">xml</a> <a href="../../questions/tagged/xpath" class="post-tag grid--cell" title="show questions tagged 'xpath'" rel="tag">xpath</a> <a href="../../questions/tagged/xquery" class="post-tag grid--cell" title="show questions tagged 'xquery'" rel="tag">xquery</a> <a href="../../questions/tagged/xpath-2.0" class="post-tag grid--cell" title="show questions tagged 'xpath-2.0'" rel="tag">xpath-2.0</a> <a href="../../questions/tagged/flwor" class="post-tag grid--cell" title="show questions tagged 'flwor'" rel="tag">flwor</a> </div> <div class="started mt0"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="asked Dec 11 '11 at 22:22">asked Dec 11 '11 at 22:22</time> <a href="../../users/845888/mnky9800n" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/845888.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="mnky9800n" /> </a> <div class="s-user-card--info"> <a href="../../users/845888/mnky9800n" class="s-user-card--link">mnky9800n</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">1,113</li> <li class="s-award-bling s-award-bling__gold" title="2 gold badges">2</li> <li class="s-award-bling s-award-bling__silver" title="15 silver badges">15</li> <li class="s-award-bling s-award-bling__bronze" title="33 bronze badges">33</li> </ul> </div> </div> </div> </div> </div> </div> </div> <div class="mln24"> <div class="question-summary" id="question-summary-8155302"> <div class="statscontainer"> <div class="stats"> <div class="vote"> <div class="votes"> <span class="vote-count-post"><strong>6</strong></span> <div class="viewcount">votes</div> </div> </div> <div class="status answered-accepted"> <strong>1</strong> answer </div> </div> </div> <div class="summary"> <h3><a href="../../questions/8155302/getting-the-value-of-dc-creator-using-sql-xml" class="question-hyperlink">Getting the value of dc:creator using SQL XML</a></h3> <div class="excerpt">I am unsure how to get the value of dc:creator from an RSS-feed using SQL. This is my xml/rss-feed: <rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"> <channel> <title>Foobar RSS http://www.foobar.com/ …
Sha
  • 2,185
  • 1
  • 36
  • 61
6
votes
4 answers

How to get child elements but none of their children?

Using MarkLogic but this is probably a generic XQuery question. I have: Random ....more children
bethesdaboys
  • 1,797
  • 7
  • 22
  • 35
6
votes
2 answers

Scope and statically known namespaces in XQuery

consider a library module ctx xquery version "3.1"; module namespace ctx="module/ctx"; declare function ctx:resolve ( $ctx as function(xs:string) as xs:QName ) as function(xs:string, xs:integer) as function(*)? { function ($name as…
line-o
  • 1,885
  • 3
  • 16
  • 33
6
votes
7 answers

Screen scraping: regular expressions or XQuery expressions?

I was answering some quiz questions for an interview, and the question was about how would I do screen scraping. That is, picking content out of a web page, assuming you don't have a better structured way to query the information directly (e.g. a…
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
6
votes
4 answers

XSLT - select nodes that came after another node

I'm trying to select all nodes that 1) come after a node with a particular property and 2) have a particular property themselves. So if I had the following XML:
Sarah Vessels
  • 30,930
  • 33
  • 155
  • 222
6
votes
3 answers

How can I format a decimal in xquery?

I'm trying to format decimals in XQuery. The decimals are currency, so the format should be ,###.##. For example: 5573652.23 should be 5,573,652.23 and 352769 should be 352,769 (or 352,769.00 if it's easier/cleaner) Right now I'm using this…
Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
6
votes
5 answers

Can XPath and XQuery work on HTML documents?

I heard that an HTML document is not a XML document from https://stackoverflow.com/a/39560454. XPath and XQuery work on XML documents. Can they work on HTML documents, and why? Although I don't know why, I guess XPath can work on HTML documens,…
Tim
  • 1
  • 141
  • 372
  • 590
6
votes
3 answers

Select text from a node and omit child nodes

I need to select the text in a node, but not any child nodes. the xml looks like this apples pears If I select a/text(), all I get is "apples". How would I retreive "apples pears" while omitting
Dave
  • 63
  • 1
  • 3
6
votes
3 answers

XPTY0004: Required item type of first operand of '>' is numeric; supplied value has item type xs:string

The toComplie string contains all the definitions of the functions like sum, multiply, etc. appended by if ($a > 0) then (iaf:numeric-equal(iaf:numeric-multiply($b, $c), $d)) else (true()) The snippet executing this is : XQueryExecutable…
VijayD
  • 826
  • 1
  • 11
  • 33