Introduction to Web Technologies and Architectures · Web Technology

Query Languages for XML

Watch lectures

Query Languages for XML

1. Why Query XML?

XML documents are hierarchical trees, not flat tables — SQL cannot directly query them. XML query languages select nodes, filter by conditions, join documents, and construct new XML/HTML results. The W3C standard pair is XPath + XQuery.

Language Scope Notes
XPath Locate nodes in one document (paths, predicates) Basis of XSLT, XQuery
XQuery Full query language over XML (FLWOR, joins, aggregation) W3C standard, SQL-for-XML
XSLT Transformation (can act as a query + report generator) covered earlier
XPointer / XLink Address points inside / link between XML documents hyperlinking
XML:DB / XDM APIs Programmatic access (Java, .NET) DOM/SAX + query engines
SQL/XML SQL extension producing/consuming XML in RDBMS XMLQuery(), FOR XML

2. XPath — Locating Nodes

An XPath expression is a path through the tree, with axes and predicates:

 /class/student/name            absolute path
 //student[@roll='101']          predicate on attribute
 //student[marks > 80]/name      filtered children
 //book[position() <= 3]         first three books
  • Axes: child:: (default), parent::, descendant::, ancestor::, attribute:: (@), following-sibling::
  • Functions: position(), last(), count(), contains(), starts-with(), sum()
  • Result of evaluating XPath = a node-set (or string/number/boolean).

3. XQuery — FLWOR Expressions

The core construct is FLWOR = For, Let, Where, Order by, Return (like SQL's SELECT-FROM-WHERE).

(: students with marks > 80, sorted descending :)
for   $s in doc("class.xml")//student
let   $total := sum($s/marks)
where $total > 80
order by $total descending
return  <result>
          <name>{ $s/name/text() }</name>
          <total>{ $total }</total>
        </result>
Clause Role SQL analogue
for iterate over a sequence FROM
let bind a variable (computed column)
where filter WHERE
order by sort ORDER BY
return construct output SELECT list

XQuery can also join two documents, aggregate (count, avg), and construct arbitrary XML output — it is to XML what SQL is to relational tables.

4. XPath/XQuery Data Model

Everything is a sequence of items: nodes (element, attribute, text, comment, PI, document) or atomic values. XPath 2.0+ shares the XDM data model and type system (with XML Schema types) with XQuery — same engine, different surface syntax.

5. SQL/XML (querying from a relational DB)

SELECT XMLELEMENT(NAME "student",
         XMLATTRIBUTES(roll AS "roll"), name)
FROM students WHERE branch = 'IT';

-- or query XML stored in a column
SELECT XMLQuery('$d/student[name="Asha"]' PASSING doc AS "d") FROM t;

Modern RDBMS (Oracle, SQL Server FOR XML, PostgreSQL xpath()) can both publish relational data as XML and query XML stored inside tables.

6. Use Cases

Search/filter inside XML repositories and CMS; extracting data for B2B integration; publishing reports from XML in HTML; querying web-service messages (SOAP); storing semi-structured data in XML-enabled databases.

7. Exam Points

  • Why are special query languages needed for XML? List them — 5 marks.
  • Explain XPath expressions with axes and predicates (examples) — 7 marks.
  • Explain the FLWOR structure of XQuery with an example — 10 marks (very frequent).
  • Compare XQuery with SQL — 5 marks.
Official AppAndroid App

PYQDeck for Android

Solve BEU previous-year exam papers anywhere, anytime. Fast search, offline paper viewing, and step-by-step solutions designed specifically for your phone.

← All Web Technology modules