You are on page 1of 23

XQuery Introduction

SITE SEARCH
HOME Introduction to XQuery
...
XQuery Basic
XQuery HOME About W3Schools
XQuery Intro W3Schools Forum
XQuery Example
XQuery FLWOR The best way to explain XQuery is to say that XQuery is to XML what SQL is to
XQuery HTML database tables. Web based charting
XQuery Terms for ASP.NET
XQuery Syntax XQuery is designed to query XML data - not just XML files, but anything that can
WEB HOSTING
appear as XML, including databases.
XQuery Advanced $15 Domain Name
XQuery Add Registration
XQuery Select Save $20 / year!
XQuery Functions
UK Domain Names
XQuery Summary What You Should Already Know
UK Web Hosting
XQuery Reference Alojamiento Web
Before you continue you should have a basic understanding of the following:
XQuery Reference Buy UK Domain Names
Register Domain Names
Selected Reading ● HTML / XHTML
Web Statistics ● XML / XML Namespaces Cheap Domain Names
Web Glossary ● XPath Cheap Web Hosting
Web Hosting Best Web Hosting
Web Quality If you want to study these subjects first, find the tutorials on our Home page.
Bulk Domain Names
W3Schools Forum UK Web Hosting
Helping W3Schools PHP MySQL Hosting
What is XQuery? Web Hosting UK
Top 10 Web Hosting
● XQuery is the language for querying XML data Web Hosting Providers
● XQuery for XML is like SQL for databases
● XQuery is built on XPath expressions ASP.NET Web Hosting
● XQuery is supported by all the major database engines (IBM, Oracle, Microsoft, etc.) 3 Months Free!
● XQuery is a W3C Recommendation
Programming Forums
Windows Web Hosting
WEB BUILDING
XML Website Tools
XQuery is About Querying XML
Website Templates
Flash Templates
XQuery is a language for finding and extracting elements and attributes from XML documents.
Website Builder
Internet Business
Here is an example of a question that XQuery could solve:
Opportunity
Custom Programming
"Select all CD records with a price less than $10 from the CD collection stored in the XML
document called cd_catalog.xml" BEST Flash Templates
Web Design Forum
Programming Forum
http://www.w3schools.com/xquery/xquery_intro.asp (1 of 3) [3/14/2007 1:10:11 PM]
XQuery Introduction

FREE Trial or Demo


XQuery and XPath
Web Content Manager
Forms,Web Alerts,RSS
XQuery 1.0 and XPath 2.0 share the same data model and support the same functions and
operators. If you have already studied XPath you will have no problems with understanding HTML Exam
XQuery. SHOPPING
UK Wholesalers
You can read more about XPath in our XPath Tutorial. UK Wholesale
ONLINE DEGREE
Advance Your Career
with Drexel University
XQuery - Examples of Use Bachelor's & Master's
Tech Degrees ONLINE

XQuery can be used to:

● Extract information to use in a Web Service


● Generate summary reports
● Transform XML data to XHTML
● Search Web documents for relevant information

XQuery is a W3C Recommendation

XQuery is compatible with several W3C standards, such as XML, Namespaces, XSLT, XPath,
and XML Schema.

XQuery 1.0 became a W3C Recommendation January 23, 2007.

To read more about the XQuery activity at W3C, please read our W3C Tutorial.

Your browser does not support inline frames or is currently configured not to display inline
frames.

Want To Be A Web Master?

If you want to be a Web Master, you will have to host your web site with an ISP (Internet
Service Provider).

MaximumASP offers seven different configurations of dedicated servers to meet your Windows
and .NET hosting needs. Hosted on our multi-tiered Enterprise Class network, these servers
provide the performance, security and reliability you need to host your high end web sites and
applications.

Visit MaximumASP

http://www.w3schools.com/xquery/xquery_intro.asp (2 of 3) [3/14/2007 1:10:11 PM]


XQuery Example

Your browser does not support inline frames or is currently configured not to display inline frames.

SITE SEARCH
HOME XQuery Example
...
XQuery Basic
XQuery HOME About W3Schools
XQuery Intro W3Schools Forum
XQuery Example
XQuery FLWOR Let's try to learn some basic XQuery syntax by looking at an example.
XQuery HTML Web based charting
XQuery Terms for ASP.NET
XQuery Syntax
WEB HOSTING
XQuery Advanced The XML Example Document $15 Domain Name
XQuery Add Registration
XQuery Select We will use the following XML document in the examples below. Save $20 / year!
XQuery Functions
UK Domain Names
XQuery Summary
"books.xml": UK Web Hosting
XQuery Reference Alojamiento Web
XQuery Reference <?xml version="1.0" encoding="ISO-8859-1"?> Buy UK Domain Names
Register Domain Names
Selected Reading
Web Statistics <bookstore> Cheap Domain Names
Web Glossary Cheap Web Hosting
Web Hosting <book category="COOKING"> Best Web Hosting
Web Quality <title lang="en">Everyday Italian</title>
Bulk Domain Names
<author>Giada De Laurentiis</author>
W3Schools Forum UK Web Hosting
<year>2005</year>
Helping W3Schools
<price>30.00</price> PHP MySQL Hosting
</book>
Web Hosting UK
Top 10 Web Hosting
<book category="CHILDREN">
<title lang="en">Harry Potter</title> Web Hosting Providers
<author>J K. Rowling</author> ASP.NET Web Hosting
<year>2005</year> 3 Months Free!
<price>29.99</price>
</book> Programming Forums
Windows Web Hosting

<book category="WEB"> WEB BUILDING


<title lang="en">XQuery Kick Start</title> XML Website Tools
<author>James McGovern</author> Website Templates
<author>Per Bothner</author> Flash Templates
<author>Kurt Cagle</author> Website Builder
<author>James Linn</author>
Internet Business
<author>Vaidyanathan Nagarajan</author>
Opportunity
<year>2003</year>
<price>49.99</price> Custom Programming
</book> BEST Flash Templates
Web Design Forum
<book category="WEB">
Programming Forum
http://www.w3schools.com/xquery/xquery_example.asp (1 of 3) [3/14/2007 1:10:29 PM]
XQuery Example
<title lang="en">Learning XML</title>
FREE Trial or Demo
<author>Erik T. Ray</author>
Web Content Manager
<year>2003</year>
Forms,Web Alerts,RSS
<price>39.95</price>
</book> HTML Exam
SHOPPING
</bookstore> UK Wholesalers
UK Wholesale

View the "books.xml" file in your browser. ONLINE DEGREE


Advance Your Career
with Drexel University
Bachelor's & Master's
Tech Degrees ONLINE
How to Select Nodes From "books.xml"?

Functions

XQuery uses functions to extract data from XML documents.

The doc() function is used to open the "books.xml" file:

doc("books.xml")

Path Expressions

XQuery uses path expressions to navigate through elements in an XML document.

The following path expression is used to select all the title elements in the "books.xml" file:

doc("books.xml")/bookstore/book/title

(/bookstore selects the bookstore element, /book selects all the book elements under the
bookstore element, and /title selects all the title elements under each book element)

The XQuery above will extract the following:

<title lang="en">Everyday Italian</title>


<title lang="en">Harry Potter</title>
<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>

Predicates

XQuery uses predicates to limit the extracted data from XML documents.

The following predicate is used to select all the book elements under the bookstore element
that have a price element with a value that is less than 30:

doc("books.xml")/bookstore/book[price<30]

http://www.w3schools.com/xquery/xquery_example.asp (2 of 3) [3/14/2007 1:10:29 PM]


XQuery Example

The XQuery above will extract the following:

<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>

Your browser does not support inline frames or is currently configured not to display inline
frames.

Want To Be A Web Master?

If you want to be a Web Master, you will have to host your web site with an ISP (Internet
Service Provider).

MaximumASP offers seven different configurations of dedicated servers to meet your Windows
and .NET hosting needs. Hosted on our multi-tiered Enterprise Class network, these servers
provide the performance, security and reliability you need to host your high end web sites and
applications.

Visit MaximumASP

Jump to: Top of Page or HOME or Printer friendly page

W3Schools provides material for training only. We do not warrant the correctness of its
contents. The risk from using it lies entirely with the user. While using this site, you agree to
have read and accepted our terms of use and privacy policy.

Copyright 1999-2007 by Refsnes Data. All Rights Reserved.

W3Schools was converted to XHTML


in December 1999

http://www.w3schools.com/xquery/xquery_example.asp (3 of 3) [3/14/2007 1:10:29 PM]


XQuery FLWOR Expressions

SITE SEARCH
HOME XQuery FLWOR Expressions
...
XQuery Basic
XQuery HOME About W3Schools
XQuery Intro W3Schools Forum
XQuery Example
XQuery FLWOR
The XML Example Document
XQuery HTML Domain Name
XQuery Terms Registration & More!
We will use the "books.xml" document in the examples below (same XML file as in the previous
XQuery Syntax
chapter). WEB HOSTING
XQuery Advanced $15 Domain Name
XQuery Add View the "books.xml" file in your browser. Registration
XQuery Select Save $20 / year!
XQuery Functions
UK Domain Names
XQuery Summary
UK Web Hosting
XQuery Reference How to Select Nodes From "books.xml" With FLWOR Alojamiento Web
XQuery Reference Buy UK Domain Names
Look at the following path expression: Register Domain Names
Selected Reading
Web Statistics Cheap Domain Names
Web Glossary doc("books.xml")/bookstore/book[price>30]/title Cheap Web Hosting
Web Hosting Best Web Hosting
Web Quality
The expression above will select all the title elements under the book elements that are under Bulk Domain Names
W3Schools Forum the bookstore element that have a price element with a value that is higher than 30.
UK Web Hosting
Helping W3Schools PHP MySQL Hosting
The following FLWOR expression will select exactly the same as the path expression above:
Web Hosting UK
Top 10 Web Hosting
for $x in doc("books.xml")/bookstore/book
where $x/price>30 Web Hosting Providers
return $x/title
ASP.NET Web Hosting
3 Months Free!
The result will be: Programming Forums
Windows Web Hosting
<title lang="en">XQuery Kick Start</title> WEB BUILDING
<title lang="en">Learning XML</title> XML Website Tools
Website Templates
With FLWOR you can sort the result: Flash Templates
Website Builder
Internet Business
for $x in doc("books.xml")/bookstore/book
Opportunity
where $x/price>30
order by $x/title Custom Programming
return $x/title
BEST Flash Templates
Web Design Forum
FLWOR is an acronym for "For, Let, Where, Order by, Return". Programming Forum
http://www.w3schools.com/xquery/xquery_flwor.asp (1 of 3) [3/14/2007 1:10:45 PM]
XQuery FLWOR Expressions

FREE Trial or Demo


The for clause selects all book elements under the bookstore element into a variable called $x. Web Content Manager
Forms,Web Alerts,RSS
The where clause selects only book elements with a price element with a value greater than
HTML Exam
30.
SHOPPING
UK Wholesalers
The order by clause defines the sort-order. Will be sort by the title element.
UK Wholesale
ONLINE DEGREE
The return clause specifies what should be returned. Here it returns the title elements.
Advance Your Career
with Drexel University
The result of the XQuery expression above will be: Bachelor's & Master's
Tech Degrees ONLINE
<title lang="en">Learning XML</title>
<title lang="en">XQuery Kick Start</title>

Your browser does not support inline frames or is currently configured not to display inline
frames.

What do you want your Web site to do?

What do you want your Web site to do? Ektron CMS400.NET lets you do everything you
need to do on the Web and do everything you want to do.

Ektron CMS400.NET gives you all the features you would expect from an
enterprise CMS, but also high-demand functionality like real-time Web
site analytics, community building, blogging, personalization, and online
calendars. Ektron CMS400.NET bridges your Web site gaps.

Use Ektron CMS400.NET for:

● Content & Document Management - Author/edit content,


manage navigation, menus, audit trails, workflow, approvals
● Personalzation & Portal Support - Allow visitors to create their own view of your
web page
● 508 & W3C Compliance Checker - Ensure compliance with scheduled or on-demand
compliance reports
● Memberships - Build online communties with blogs and forums, enable email web
alerts, build polls and surveys
● Web Site Analytics - track visitors, report on most requested pages, see where they
came from and where they are going
● HTML Form Engine - Create a two-way dialog with visitors using our wizard-like
HTML form-building engine with validation and database support
● Multilingual Support - Integrated translation tools help you reach multiple audiences
by speaking their language
● Online Calendars - Build and manage Web-based calendars and enable users to
customize a view based on what’s important to them
http://www.w3schools.com/xquery/xquery_flwor.asp (2 of 3) [3/14/2007 1:10:45 PM]
XQuery FLWOR + HTML

SITE SEARCH
HOME XQuery FLWOR + HTML
...
XQuery Basic
XQuery HOME About W3Schools
XQuery Intro W3Schools Forum
XQuery Example
XQuery FLWOR
The XML Example Document
XQuery HTML Ecommerce
XQuery Terms Components
We will use the "books.xml" document in the examples below (same XML file as in the previous
XQuery Syntax for ASP.NET
chapters).
XQuery Advanced WEB HOSTING
XQuery Add View the "books.xml" file in your browser. $15 Domain Name
XQuery Select Registration
XQuery Functions Save $20 / year!
XQuery Summary
UK Domain Names
XQuery Reference Present the Result In an HTML List UK Web Hosting
XQuery Reference Alojamiento Web

Look at the following XQuery FLWOR expression: Buy UK Domain Names


Selected Reading
Register Domain Names
Web Statistics
Web Glossary for $x in doc("books.xml")/bookstore/book/title Cheap Domain Names
Web Hosting order by $x Cheap Web Hosting
Web Quality return $x Best Web Hosting
W3Schools Forum Bulk Domain Names
The expression above will select all the title elements under the book elements that are under
UK Web Hosting
Helping W3Schools the bookstore element, and return the title elements in alphabetical order.
PHP MySQL Hosting
Now we want to list all the book-titles in our bookstore in an HTML list. We add <ul> and <li> Web Hosting UK
tags to the FLWOR expression: Top 10 Web Hosting
Web Hosting Providers
<ul>
{ ASP.NET Web Hosting
for $x in doc("books.xml")/bookstore/book/title 3 Months Free!
order by $x Programming Forums
return <li>{$x}</li> Windows Web Hosting
}
</ul> WEB BUILDING
XML Website Tools
Website Templates
The result of the above will be: Flash Templates
Website Builder
<ul> Internet Business
<li><title lang="en">Everyday Italian</title></li> Opportunity
<li><title lang="en">Harry Potter</title></li>
Custom Programming
<li><title lang="en">Learning XML</title></li>
<li><title lang="en">XQuery Kick Start</title></li> BEST Flash Templates
</ul>

http://www.w3schools.com/xquery/xquery_flwor_html.asp (1 of 2) [3/14/2007 1:10:55 PM]


XQuery FLWOR + HTML

Web Design Forum


Now we want to eliminate the title element, and show only the data inside the title element: Programming Forum
FREE Trial or Demo
<ul> Web Content Manager
{ Forms,Web Alerts,RSS
for $x in doc("books.xml")/bookstore/book/title HTML Exam
order by $x
return <li>{data($x)}</li> SHOPPING
} UK Wholesalers
</ul> UK Wholesale
ONLINE DEGREE
Advance Your Career
The result will be (an HTML list):
with Drexel University
Bachelor's & Master's
<ul> Tech Degrees ONLINE
<li>Everyday Italian</li>
<li>Harry Potter</li>
<li>Learning XML</li>
<li>XQuery Kick Start</li>
</ul>

Your browser does not support inline frames or is currently configured not to display inline
frames.

Learn XML with Stylus Studio XML Tools - Free Download!

Stylus Studio makes it easy to edit and validate XML, XSLT,


DTD, XML Schema, XHTML, XPath, XQuery and Web Service
applications.

Free XML video demonstrations will help you learn XML and
become a W3C XML development master in just minutes!

Download a FREE Trial Now!

Jump to: Top of Page or HOME or Printer friendly page

W3Schools provides material for training only. We do not warrant the correctness of its
contents. The risk from using it lies entirely with the user. While using this site, you agree to
have read and accepted our terms of use and privacy policy.

Copyright 1999-2007 by Refsnes Data. All Rights Reserved.

W3Schools was converted to XHTML


in December 1999

http://www.w3schools.com/xquery/xquery_flwor_html.asp (2 of 2) [3/14/2007 1:10:55 PM]


XQuery Terms

SITE SEARCH
HOME XQuery Terms
...
XQuery Basic
XQuery HOME About W3Schools
XQuery Intro W3Schools Forum
XQuery Example
XQuery FLWOR In XQuery, there are seven kinds of nodes: element, attribute, text, namespace,
XQuery HTML processing-instruction, comment, and document (root) nodes. Domain Name
XQuery Terms Registration & More!
XQuery Syntax
WEB HOSTING
XQuery Advanced $15 Domain Name
XQuery Add XQuery Terminology Registration
XQuery Select Save $20 / year!
XQuery Functions Nodes UK Domain Names
XQuery Summary
UK Web Hosting
XQuery Reference In XQuery, there are seven kinds of nodes: element, attribute, text, namespace, processing- Alojamiento Web
XQuery Reference instruction, comment, and document (root) nodes. XML documents are treated as trees of
Buy UK Domain Names
nodes. The root of the tree is called the document node (or root node).
Register Domain Names
Selected Reading
Web Statistics Look at the following XML document: Cheap Domain Names
Web Glossary Cheap Web Hosting
Web Hosting Best Web Hosting
<?xml version="1.0" encoding="ISO-8859-1"?>
Web Quality
Bulk Domain Names
W3Schools Forum <bookstore> UK Web Hosting
Helping W3Schools PHP MySQL Hosting
<book> Web Hosting UK
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author> Top 10 Web Hosting
<year>2005</year> Web Hosting Providers
<price>29.99</price>
ASP.NET Web Hosting
</book>
3 Months Free!

</bookstore> Programming Forums


Windows Web Hosting
WEB BUILDING
Example of nodes in the XML document above: XML Website Tools
Website Templates
<bookstore> (document node) Flash Templates
Website Builder
<author>J K. Rowling</author> (element node) Internet Business
Opportunity
lang="en" (attribute node) Custom Programming
BEST Flash Templates
Atomic values Web Design Forum
Programming Forum
http://www.w3schools.com/xquery/xquery_terms.asp (1 of 4) [3/14/2007 1:11:05 PM]
XQuery Terms

Atomic values are nodes with no children or parent. FREE Trial or Demo
Web Content Manager
Example of atomic values: Forms,Web Alerts,RSS
HTML Exam
J K. Rowling SHOPPING
UK Wholesalers
"en" UK Wholesale
ONLINE DEGREE
Advance Your Career
Items with Drexel University
Bachelor's & Master's
Items are atomic values or nodes. Tech Degrees ONLINE

Relationship of Nodes

Parent

Each element and attribute has one parent.

In the following example; the book element is the parent of the title, author, year, and price:

<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>

Children

Element nodes may have zero, one or more children.

In the following example; the title, author, year, and price elements are all children of the
book element:

<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>

Siblings

Nodes that have the same parent.

In the following example; the title, author, year, and price elements are all siblings:

http://www.w3schools.com/xquery/xquery_terms.asp (2 of 4) [3/14/2007 1:11:05 PM]


XQuery Terms

<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>

Ancestors

A node's parent, parent's parent, etc.

In the following example; the ancestors of the title element are the book element and the
bookstore element:

<bookstore>

<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>

</bookstore>

Descendants

A node's children, children's children, etc.

In the following example; descendants of the bookstore element are the book, title, author,
year, and price elements:

<bookstore>

<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>

</bookstore>

Your browser does not support inline frames or is currently configured not to display inline
frames.

What do you want your Web site to do?

http://www.w3schools.com/xquery/xquery_terms.asp (3 of 4) [3/14/2007 1:11:05 PM]


XQuery Syntax

Your browser does not support inline frames or is currently configured not to display inline frames.

SITE SEARCH
HOME XQuery Syntax
...
XQuery Basic
XQuery HOME About W3Schools
XQuery Intro W3Schools Forum
XQuery Example
XQuery FLWOR XQuery is case-sensitive and XQuery elements, attributes, and variables must be
XQuery HTML valid XML names. Active Server Pages
XQuery Terms Resource Web Site
XQuery Syntax
WEB HOSTING
XQuery Advanced $15 Domain Name
XQuery Add XQuery Basic Syntax Rules Registration
XQuery Select Save $20 / year!
XQuery Functions Some basic syntax rules: UK Domain Names
XQuery Summary
UK Web Hosting
● XQuery is case-sensitive Alojamiento Web
XQuery Reference
● XQuery elements, attributes, and variables must be valid XML names
XQuery Reference Buy UK Domain Names
● An XQuery string value can be in single or double quotes
● An XQuery variable is defined with a $ followed by a name, e.g. $bookstore Register Domain Names
Selected Reading
● XQuery comments are delimited by (: and :), e.g. (: XQuery Comment :)
Web Statistics Cheap Domain Names
Web Glossary Cheap Web Hosting
Web Hosting Best Web Hosting
Web Quality
XQuery Conditional Expressions Bulk Domain Names
W3Schools Forum UK Web Hosting
Helping W3Schools "If-Then-Else" expressions are allowed in XQuery. PHP MySQL Hosting
Web Hosting UK
Look at the following example:
Top 10 Web Hosting
Web Hosting Providers
for $x in doc("books.xml")/bookstore/book
return if ($x/@category="CHILDREN") ASP.NET Web Hosting
then <child>{data($x/title)}</child> 3 Months Free!
else <adult>{data($x/title)}</adult> Programming Forums
Windows Web Hosting
Notes on the "if-then-else" syntax: parentheses around the if expression are required. else WEB BUILDING
is required, but it can be just else (). XML Website Tools
Website Templates
The result of the example above will be: Flash Templates
Website Builder
<adult>Everyday Italian</adult> Internet Business
<child>Harry Potter</child> Opportunity
<adult>Learning XML</adult> Custom Programming
<adult>XQuery Kick Start</adult>
BEST Flash Templates
Web Design Forum
Programming Forum
http://www.w3schools.com/xquery/xquery_syntax.asp (1 of 3) [3/14/2007 1:11:15 PM]
XQuery Syntax

XQuery Comparisons FREE Trial or Demo


Web Content Manager
In XQuery there are two ways of comparing values. Forms,Web Alerts,RSS
HTML Exam
1. General comparisons: =, !=, <, <=, >, >=
SHOPPING
UK Wholesalers
2. Value comparisons: eq, ne, lt, le, gt, ge UK Wholesale
ONLINE DEGREE
The difference between the two comparison methods are shown below. Advance Your Career
with Drexel University
Look at the following XQuery expressions: Bachelor's & Master's
Tech Degrees ONLINE

$bookstore//book/@q > 10

The expression above returns true if any q attributes


have values greater than 10.

$bookstore//book/@q gt 10

The expression above returns true if there is only one


q attribute returned by the expression, and its value
is greater than 10. If more than one q is returned,
an error occurs.

Your browser does not support inline frames or is currently configured not to display inline
frames.

What do you want your Web site to do?

What do you want your Web site to do? Ektron CMS400.NET lets you do everything you
need to do on the Web and do everything you want to do.

Ektron CMS400.NET gives you all the features you would expect from an
enterprise CMS, but also high-demand functionality like real-time Web
site analytics, community building, blogging, personalization, and online
calendars. Ektron CMS400.NET bridges your Web site gaps.

Use Ektron CMS400.NET for:

● Content & Document Management - Author/edit content,


manage navigation, menus, audit trails, workflow, approvals
● Personalzation & Portal Support - Allow visitors to create their own view of your
web page
http://www.w3schools.com/xquery/xquery_syntax.asp (2 of 3) [3/14/2007 1:11:15 PM]
XQuery Adding Elements and Attributes

SITE SEARCH
HOME XQuery Adding Elements and Attributes
...
XQuery Basic
XQuery HOME About W3Schools
XQuery Intro W3Schools Forum
XQuery Example
XQuery FLWOR
The XML Example Document
XQuery HTML Ecommerce
XQuery Terms Components
We will use the "books.xml" document in the examples below (same XML file as in the previous
XQuery Syntax for ASP.NET
chapters).
XQuery Advanced WEB HOSTING
XQuery Add View the "books.xml" file in your browser. $15 Domain Name
XQuery Select Registration
XQuery Functions Save $20 / year!
XQuery Summary
UK Domain Names
XQuery Reference Adding Elements and Attributes to the Result UK Web Hosting
XQuery Reference Alojamiento Web

As we have seen in a previous chapter, we may include elements and attributes from the input Buy UK Domain Names
Selected Reading
document ("books.xml) in the result: Register Domain Names
Web Statistics
Web Glossary Cheap Domain Names
Web Hosting for $x in doc("books.xml")/bookstore/book/title Cheap Web Hosting
Web Quality order by $x
Best Web Hosting
return $x
W3Schools Forum Bulk Domain Names
UK Web Hosting
Helping W3Schools The XQuery expression above will include both the title element and the lang attribute in the
result, like this: PHP MySQL Hosting
Web Hosting UK
<title lang="en">Everyday Italian</title> Top 10 Web Hosting
<title lang="en">Harry Potter</title>
Web Hosting Providers
<title lang="en">Learning XML</title>
<title lang="en">XQuery Kick Start</title> ASP.NET Web Hosting
3 Months Free!

The XQuery expression above returns the title elements the exact same way as they are Programming Forums
described in the input document. Windows Web Hosting
WEB BUILDING
We now want to add our own elements and attributes to the result! XML Website Tools
Website Templates
Add HTML Elements and Text Flash Templates
Website Builder
Now, we want to add some HTML elements to the result. We will put the result in an HTML list Internet Business
- together with some text: Opportunity
Custom Programming
BEST Flash Templates

http://www.w3schools.com/xquery/xquery_add.asp (1 of 4) [3/14/2007 1:11:26 PM]


XQuery Adding Elements and Attributes

<html> Web Design Forum


<body> Programming Forum
FREE Trial or Demo
<h1>Bookstore</h1> Web Content Manager
Forms,Web Alerts,RSS
<ul> HTML Exam
{
for $x in doc("books.xml")/bookstore/book SHOPPING
UK Wholesalers
order by $x/title
UK Wholesale
return <li>{data($x/title)}. Category: {data($x/@category)}</li>
} ONLINE DEGREE
</ul> Advance Your Career
with Drexel University
</body> Bachelor's & Master's
</html> Tech Degrees ONLINE

The XQuery expression above will generate the following result:

<html>
<body>

<h1>Bookstore</h1>

<ul>
<li>Everyday Italian. Category: COOKING</li>
<li>Harry Potter. Category: CHILDREN</li>
<li>Learning XML. Category: WEB</li>
<li>XQuery Kick Start. Category: WEB</li>
</ul>

</body>
</html>

Add Attributes to HTML Elements

Next, we want to use the category attribute as a class attribute in the HTML list:

<html>
<body>

<h1>Bookstore</h1>

<ul>
{
for $x in doc("books.xml")/bookstore/book
order by $x/title
return <li class="{data($x/@category)}">{data($x/title)}</li>
}
</ul>

</body>

http://www.w3schools.com/xquery/xquery_add.asp (2 of 4) [3/14/2007 1:11:26 PM]


XQuery Adding Elements and Attributes
</html>

The XQuery expression above will generate the following result:

<html>
<body>

<h1>Bookstore</h1>

<ul>
<li class="COOKING">Everyday Italian</li>
<li class="CHILDREN">Harry Potter</li>
<li class="WEB">Learning XML</li>
<li class="WEB">XQuery Kick Start</li>
</ul>

</body>
</html>

Your browser does not support inline frames or is currently configured not to display inline
frames.

NEW! Altova MissionKit 2007– Save ½ off Intelligent tools for XML
developers & software architects

The Altova MissionKit 2007 bundles Altova’s application


development, data management and modeling tools at 50%
off their regular prices. It is available in a variety of
configurations tailored to meet the needs of software
architects and XML developers. All MissionKits include the
world’s leading XML development tools: Altova XMLSpy,
MapForce, and StyleVision.

Gear up; download a FREE 30-day trial today!

http://www.w3schools.com/xquery/xquery_add.asp (3 of 4) [3/14/2007 1:11:26 PM]


XQuery Selecting and Filtering

Your browser does not support inline frames or is currently configured not to display inline frames.

SITE SEARCH
HOME XQuery Selecting and Filtering
...
XQuery Basic
XQuery HOME About W3Schools
XQuery Intro W3Schools Forum
XQuery Example
XQuery FLWOR
The XML Example Document
XQuery HTML Ecommerce
XQuery Terms Components
We will use the "books.xml" document in the examples below (same XML file as in the previous
XQuery Syntax for ASP.NET
chapters).
XQuery Advanced WEB HOSTING
XQuery Add View the "books.xml" file in your browser. $15 Domain Name
XQuery Select Registration
XQuery Functions Save $20 / year!
XQuery Summary
UK Domain Names
XQuery Reference Selecting and Filtering Elements UK Web Hosting
XQuery Reference Alojamiento Web

As we have seen in the previous chapters, we are selecting and filtering elements with either a Buy UK Domain Names
Selected Reading
Path expression or with a FLWOR expression. Register Domain Names
Web Statistics
Web Glossary Cheap Domain Names
Web Hosting Look at the following FLWOR expression: Cheap Web Hosting
Web Quality Best Web Hosting
for $x in doc("books.xml")/bookstore/book
W3Schools Forum Bulk Domain Names
where $x/price>30
order by $x/title UK Web Hosting
Helping W3Schools
return $x/title
PHP MySQL Hosting
Web Hosting UK
● for - (optional) binds a variable to each item returned by the in expression Top 10 Web Hosting
● let - (optional)
● where - (optional) specifies a criteria Web Hosting Providers
● order by - (optional) specifies the sort-order of the result ASP.NET Web Hosting
● return - specifies what to return in the result
3 Months Free!
Programming Forums
The for Clause
Windows Web Hosting
WEB BUILDING
The for clause binds a variable to each item returned by the in expression. The for clause
XML Website Tools
results in iteration. There can be multiple for clauses in the same FLWOR expression.
Website Templates
To loop a specific number of times in a for clause, you may use the to keyword: Flash Templates
Website Builder
Internet Business
for $x in (1 to 5) Opportunity
return <test>{$x}</test>
Custom Programming
BEST Flash Templates
Result:

http://www.w3schools.com/xquery/xquery_select.asp (1 of 4) [3/14/2007 1:11:36 PM]


XQuery Selecting and Filtering

<test>1</test> Web Design Forum


<test>2</test> Programming Forum
<test>3</test> FREE Trial or Demo
<test>4</test> Web Content Manager
<test>5</test> Forms,Web Alerts,RSS
HTML Exam
The at keyword can be used to count the iteration: SHOPPING
UK Wholesalers
for $x at $i in doc("books.xml")/bookstore/book/title UK Wholesale
return <book>{$i}. {data($x)}</book> ONLINE DEGREE
Advance Your Career
with Drexel University
Result:
Bachelor's & Master's
Tech Degrees ONLINE
<book>1. Everyday Italian</book>
<book>2. Harry Potter</book>
<book>3. XQuery Kick Start</book>
<book>4. Learning XML</book>

It is also allowed with more than one in expression in the for clause. Use comma to separate
each in expression:

for $x in (10,20), $y in (100,200)


return <test>x={$x} and y={$y}</test>

Result:

<test>x=10 and y=100</test>


<test>x=10 and y=200</test>
<test>x=20 and y=100</test>
<test>x=20 and y=200</test>

The let Clause

The let clause allows variable assignments and it avoids repeating the same expression many
times. The let clause does not result in iteration.

let $x := (1 to 5)
return <test>{$x}</test>

Result:

<test>1 2 3 4 5</test>

The where Clause

The where clause is used to specify one or more criteria for the result:

where $x/price>30 and $x/price<100

http://www.w3schools.com/xquery/xquery_select.asp (2 of 4) [3/14/2007 1:11:36 PM]


XQuery Selecting and Filtering

The order by Clause

The order by clause is used to specify the sort order of the result. Here we want to order the
result by category and title:

for $x in doc("books.xml")/bookstore/book
order by $x/@category, $x/title
return $x/title

Result:

<title lang="en">Harry Potter</title>


<title lang="en">Everyday Italian</title>
<title lang="en">Learning XML</title>
<title lang="en">XQuery Kick Start</title>

The return Clause

The return clause specifies what is to be returned.

for $x in doc("books.xml")/bookstore/book
return $x/title

Result:

<title lang="en">Everyday Italian</title>


<title lang="en">Harry Potter</title>
<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>

Your browser does not support inline frames or is currently configured not to display inline
frames.

NEW! Altova MissionKit 2007– Save ½ off Intelligent tools for XML
developers & software architects

The Altova MissionKit 2007 bundles Altova’s application


development, data management and modeling tools at 50%
off their regular prices. It is available in a variety of
configurations tailored to meet the needs of software
architects and XML developers. All MissionKits include the
world’s leading XML development tools: Altova XMLSpy,
MapForce, and StyleVision.

Gear up; download a FREE 30-day trial today!

http://www.w3schools.com/xquery/xquery_select.asp (3 of 4) [3/14/2007 1:11:36 PM]


XQuery Functions

Your browser does not support inline frames or is currently configured not to display inline frames.

SITE SEARCH
HOME XQuery Functions
...
XQuery Basic
XQuery HOME About W3Schools
XQuery Intro W3Schools Forum
XQuery Example
XQuery FLWOR XQuery 1.0, XPath 2.0, and XSLT 2.0 share the same functions library.
XQuery HTML Web based charting
XQuery Terms for ASP.NET
XQuery Syntax
WEB HOSTING
XQuery Advanced XQuery Functions $15 Domain Name
XQuery Add Registration
XQuery Select XQuery includes over 100 built-in functions. There are functions for string values, numeric Save $20 / year!
XQuery Functions values, date and time comparison, node and QName manipulation, sequence manipulation, UK Domain Names
XQuery Summary Boolean values, and more. You can also define your own functions in XQuery.
UK Web Hosting
XQuery Reference Alojamiento Web
XQuery Reference Buy UK Domain Names
Register Domain Names
Selected Reading XQuery Built-in Functions
Web Statistics Cheap Domain Names
Web Glossary The URI of the XQuery function namespace is: Cheap Web Hosting
Web Hosting http://www.w3.org/2005/02/xpath-functions Best Web Hosting
Web Quality
Bulk Domain Names
W3Schools Forum The default prefix for the function namespace is fn:.
UK Web Hosting
Helping W3Schools Tip: Functions are often called with the fn: prefix, such as fn:string(). However, since fn: is the PHP MySQL Hosting
default prefix of the namespace, the function names do not need to be prefixed when called. Web Hosting UK
Top 10 Web Hosting
The reference of all the built-in XQuery 1.0 functions is located in our XPath tutorial.
Web Hosting Providers
ASP.NET Web Hosting
3 Months Free!

Examples of Function Calls Programming Forums


Windows Web Hosting

A call to a function can appear where an expression may appear. Look at the examples below: WEB BUILDING
XML Website Tools

Example 1: In an element Website Templates


Flash Templates
Website Builder
<name>{uppercase($booktitle)}</name>
Internet Business
Opportunity
Example 2: In the predicate of a path expression Custom Programming
BEST Flash Templates
doc("books.xml")/bookstore/book[substring(title,1,5)='Harry']
Web Design Forum
Programming Forum
http://www.w3schools.com/xquery/xquery_functions.asp (1 of 3) [3/14/2007 1:11:47 PM]
XQuery Functions

Example 3: In a let clause FREE Trial or Demo


Web Content Manager
let $name := (substring($booktitle,1,4)) Forms,Web Alerts,RSS
HTML Exam
SHOPPING
UK Wholesalers
XQuery User-Defined Functions
UK Wholesale
ONLINE DEGREE
If you cannot find the XQuery function you need, you can write your own.
Advance Your Career
with Drexel University
User-defined functions can be defined in the query or in a separate library. Bachelor's & Master's
Tech Degrees ONLINE
Syntax

declare function prefix:function_name($parameter AS datatype)


AS returnDatatype
{

(: ...function code here... :)

};

Notes on user-defined functions:

● Use the declare function keyword


● The name of the function must be prefixed
● The data type of the parameters are mostly the same as the data types defined in XML
Schema
● The body of the function must be surrounded by curly braces

Example of a User-defined Function Declared in the Query

declare function local:minPrice(


$price as xs:decimal?,
$discount as xs:decimal?)
AS xs:decimal?
{
let $disc := ($price * $discount) div 100
return ($price - $disc)
};

(: Below is an example of how to call the function above :)

<minPrice>{local:minPrice($book/price, $book/discount)}</minPrice>

Your browser does not support inline frames or is currently configured not to display inline
frames.

http://www.w3schools.com/xquery/xquery_functions.asp (2 of 3) [3/14/2007 1:11:47 PM]


XQuery Functions

Have you ever wanted to distribute your dynamic web sites on a CD or


DVD?

DWebPro is a stand alone web server developed specifically for


distributing dynamic web sites (for example PHP/MySQL, ASP.Net/Ms
Access) on CD or DVD without needing to rewrite the web site code.

Just imagine the look on your client's face when you:

■ Browse their dynamic web sites from a CD/DVD.


■ Run to MySQL, PostgreSQL, Firebird and other databases directly from CD/DVD.
■ Send their email order directly from the CD/DVD.
■ Ship thousands of CDs and DVDs with the cost of a single license.
■ Create CDs and DVDs that run on any Windows platform.
■ And more...

Download Your Copy of DWebPro, it's "FREE for non commercial use"!

Jump to: Top of Page or HOME or Printer friendly page

W3Schools provides material for training only. We do not warrant the correctness of its
contents. The risk from using it lies entirely with the user. While using this site, you agree to
have read and accepted our terms of use and privacy policy.

Copyright 1999-2007 by Refsnes Data. All Rights Reserved.

W3Schools was converted to XHTML


in December 1999

http://www.w3schools.com/xquery/xquery_functions.asp (3 of 3) [3/14/2007 1:11:47 PM]

You might also like