XML retrieval
Practical study notes on XML retrieval: models, indexing, query languages (XPath/XQuery), tree encodings (pre/post, Dewey), and scoring elements with TF-IDF/BM25, with worked examples and exam guidance.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
XML retrieval is used when documents are structured as hierarchical tagged text (XML/HTML). Engineers use it to return the right subparts (elements) of documents — not only whole documents — for better precision in digital libraries, web archives, APIs and structured search over data such as patents, product catalogs or legal texts.
Key ideas
Start from first principles and simple definitions.
XML: eXtensible Markup Language. A plain-text format for nested, tagged data. An XML file is a rooted ordered tree of elements, attributes and text.
Element: A part of an XML tree consisting of a start-tag, content (which can include text and child elements) and an end-tag. Example: <title>Intro</title>.
Document: In XML retrieval, a document is usually one XML file. A collection is a set of documents.
Element retrieval: A retrieval task where the system returns a relevant element (a subtree) rather than the whole document. Definition: An element E is relevant to query Q if the content and/or structure of E satisfies Q’s information need (application-dependent).
Granularity: The size/level of elements returned (section, paragraph, sentence). The granularity problem: deciding which element size best matches user needs.
XPath and XQuery: Query languages for XML. XPath selects nodes using path expressions and axes (child, descendant, parent, ancestor). XQuery is a full query language (FLWOR expressions) built on XPath.
Zone / zone indexing: Treat XML tags as zones (title, abstract, body). A zone index stores separate term lists per zone. Weighted zone scoring adds weights per zone.
Element-level inverted index: Inverted index that maps (term → list of element IDs and term frequencies in that element). This supports element retrieval directly.
Path index: Indexes the XML path (sequence of tags from root to node) so queries restricting paths (e.g., /book/author) are fast.
Tree encoding: Efficiently represent tree structure for ancestor/descendant tests.
- Pre/post (interval) numbering: Do a DFS numbering: assign each node a pre (entry) number and post (exit) number. Theorem (ancestor test): Node A is an ancestor of node B iff pre(A) < pre(B) and post(A) > post(B). Assumption: numbering is done by a depth-first traversal that increments a counter on entry and exit.
- Dewey encoding: Each node gets a path label of integers separated by dots (e.g., 1.4.2). Useful for partial updates and ordered children.
Structural joins: Algorithms to evaluate ancestor-descendant and parent-child relationships between two sets of node lists (e.g., lists of nodes matching tag A and tag B). Use stack-based merge (Stack-Tree) or holistic twig joins (TwigStack), or region encoding checks (pre/post) to produce pairs (A,B) where the relationship holds.
Scoring elements: Extend TF-IDF and BM25 to element-level retrieval.
- Term frequency (tf) in element e: count of term occurrences in e (could be raw count or log-scaled). Document frequency (df): number of elements (or documents, depending on design) containing the term.
- Choice: compute idf over elements (preferred for element retrieval) or over documents (if returning documents).
INEX: A research initiative and evaluation campaign for XML retrieval. Defines tasks like focused retrieval (returning best element(s)) and full-text structural queries.
Diagrams to draw (for exams):
- Draw a rooted tree: root labelled <doc>, children <sec1>, <sec2>; <sec1> has <p1> and <p2>. Label each node with a unique id, pre and post numbers (from DFS). Show text snippets inside leaves. Use this to demonstrate ancestor tests and element selection by XPath (e.g. /doc/sec1//p2).
Formulas
Each formula on its own line. Units: unitless (no physical units).
tf(t,e) = raw count of term t in element e — tf: unitless integer, applies to counting terms inside an element.
idf(t) = ln(N / df(t)) — N is total number of elements in collection (unitless), df(t) number of elements containing t, natural log. Applies when idf is computed over elements.
idf_BM25(t) = ln((N - df(t) + 0.5) / (df(t) + 0.5)) — Robertson/Sparck Jones idf used in BM25 (natural log).
w(t,e) = tf(t,e) * idf(t) — element TF-IDF weight for term t in element e; unitless. Assumes raw tf and natural-log idf.
score_zoned(e,Q) = sum_z g_z * sum_{t in Q} tf_z(t,e) * idf(t) — weighted zone scoring; g_z ≥ 0 and sum_z g_z = 1; tf_z counts occurrences of t in zone z of element e.
BM25(e,Q) = sum_{t in Q} idf(t) * ((tf(t,e) * (k1+1)) / (tf(t,e) + k1 * (1 - b + b * |e|/avg|e|))) — where |e| is element length (terms), avg|e| average element length in collection, typical k1=1.2, b=0.75. Unitless; applies when scoring elements with BM25.
Ancestor(A,B) ⇔ pre(A) < pre(B) and post(A) > post(B) — pre/post numbering integers assigned by DFS traversal. Applies to region-encoded trees.
XPath syntax to remember (short):
/a/bchild path;//bdescendant;a[@id='x']attribute filter;text()text node;count(//p)function.
XQuery FLWOR pattern: for $x in //book where $x/author = 'K' order by $x/title return $x/title — remember FLWOR stands for For-Let-Where-Order-Return.
Worked examples
Example 1 — TF-IDF for elements (numerical, exam style)
Collection: 5 elements (N = 5). Term t appears in df(t) = 2 elements. In element e, tf(t,e) = 3 (raw count).
Formula: idf(t) = ln(N/df), w = tf * idf.
Step 1: idf = ln(5 / 2) = ln(2.5) = 0.916291.
Step 2: w = 3 * 0.916291 = 2.748873.
Answer: w(t,e) = 2.7489 (unitless).
Example 2 — BM25 for an element (numerical) Given: collection-wide total elements N = 1000 (for idf formula), term appears in df = 10 elements. For element e: tf = 4, |e| = 100 terms, average element length avg|e| = 80. Use k1 = 1.2, b = 0.75. Formula steps:
idf = ln((N - df + 0.5) / (df + 0.5)) = ln((1000 - 10 + 0.5) / (10 + 0.5)) = ln(990.5 / 10.5) = ln(94.333333) = 4.5470.- Denominator term:
tf + k1 * (1 - b + b * |e|/avg|e|)=4 + 1.2 * (1 - 0.75 + 0.75 * 100 / 80)=4 + 1.2 * (0.25 + 0.9375)=4 + 1.2 * 1.1875=4 + 1.425=5.425. - Numerator:
tf * (k1 + 1) = 4 * 2.2 = 8.8. - Term score:
8.8 / 5.425 = 1.6221. - BM25 contribution for the term:
4.5470 * 1.6221 = 7.3755. Answer: BM25 score contribution = 7.3755 (unitless).
Example 3 — Pre/post numbering and ancestor test (theory/model answer) Draw a tree and label by DFS (diagram instructions): root A with children B and C. B has children D and E. C has child F. Perform DFS and assign pre on entry, post on exit. Walkthrough:
- Enter A: pre(A) = 1.
- Enter B: pre(B) = 2.
- Enter D: pre(D) = 3. Exit D: post(D) = 4.
- Enter E: pre(E) = 5. Exit E: post(E) = 6.
- Exit B: post(B) = 7.
- Enter C: pre(C) = 8.
- Enter F: pre(F) = 9. Exit F: post(F) = 10.
- Exit C: post(C) = 11.
- Exit A: post(A) = 12. Check: Is B ancestor of E? pre(B)=2 < pre(E)=5 and post(B)=7 > post(E)=6 ⇒ true. Is C ancestor of D? pre(C)=8 < pre(D)=3? No, so false. This demonstrates the ancestor test formula above.
Common mistakes
- Using pre-order alone to test ancestry (need both pre and post or an interval scheme).
- Forgetting XML order when using Dewey codes and assuming unordered children.
- Returning overlapping elements without giving an overlap strategy (INEX requires de-overlapping rules or ranking of overlapping units).
- Misinterpreting XPath:
//a/bapplies//beforea, sobmust be a direct child ofa. To match any descendantbundera, use//a//b. Note://babbreviatesdescendant-or-self::node()/child::band matches non-direct descendants.
For your exam
Typical question types:
- Definitions and short notes (define element retrieval, zone index, Dewey encoding).
- Derivations and proofs (prove ancestor test using pre/post numbers; explain complexity of structural join).
- Numerical problems (compute TF-IDF or BM25 for elements; compute pre/post for tree and answer ancestor queries).
- Programming or pseudo-code (outline algorithm for structural join using stacks, or algorithm to build element-level inverted index).
- Diagrams (draw sample XML tree, annotate pre/post and show result of an XPath query).
What a full-marks answer includes:
- Precise definitions with bold terms (element, collection, element retrieval).
- Clear assumptions (e.g., idf computed over elements, DFS numbering used for pre/post).
- Step-by-step derivations; show intermediate numbers and units (unitless) for numericals.
- For structural algorithms: time complexity and why (e.g., structural join is linear in input lists under certain conditions) and a brief pseudocode.
Quick check
- State the ancestor test using pre/post numbers.
- Give the TF-IDF weight formula for a term in an element (use ln for idf).
- What XPath expression selects all <title> nodes anywhere in the document? Answers: 1) pre(A) < pre(B) and post(A) > post(B). 2) w = tf(t,e) * ln(N/df(t)). 3) //title
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?