Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

Data Formats (JSON, YAML, TOML, XML, CSV) Module 8

XPath, XSLT and XQuery interview questions

Interview questions on XPath, XSLT and XQuery: short warm-ups, then what each harder question tests, the first step of a good answer and its follow-ups.

6 questions: 2 warm-ups and 4 with model answers · Levels: Fresher to senior

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher to mid

    What is the difference between //book[1] and (//book)[1]?

    //book is short for /descendant-or-self::node()/child::book, so in //book[1] the predicate belongs to the child step and is tested for each parent separately: the result is the first book of every shelf, three books in the lesson's catalogue. The parentheses in (//book)[1] first build one node-set of every book in the document and then apply the predicate to that set, in document order, so the result is a single book, the first in the file. /descendant::book[1] gives the same single book, because its descendant axis numbers all the books from the root at once. The difference shows up in any document with more than one parent of the element you want.

    From the lesson XPath 1.0: paths, axes and predicates

  2. Fresher to mid

    What is the identity transform, and why start a targeted change from it?

    Answered in the lesson XSLT fundamentals: templates and identity

Questions with model answers

What each question tests, the first step of a good answer and the follow-ups to expect.

  1. Fresher to mid

    A Java library app finds books with //book[author = '<name>'], built by joining strings. Searching for Nina D'Souza shows an error page. What is wrong, how do you fix it in Java and in a PHP service that runs the same query, and how do you prove the fix?

    What it tests
    Recognising XPath injection from a harmless symptom, binding a variable in Java, quoting with PHP's own function, and a test that fails before the fix and passes after it.
    First step
    Read the error as the symptom of a query built from input: the apostrophe ends the string literal and the rest of the name is read as XPath. Compile one fixed expression with $who and supply the name through an XPathVariableResolver; in PHP 8.4 and later, build the literal with DOMXPath::quote.
    Follow-ups

    From the lesson XPath 1.0: paths, axes and predicates

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.

    Opening the model answers with your pass…

    Model answers open with any MySmartCoPilot pass.

    • Pro pass · 1 week₹79$2.99
    • 30 days₹179$5.99
    • 1 year₹1,699$45

    One payment; nothing renews unless you choose to. The price shown is the total you pay: no fees or charges are added at checkout.

    It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  2. Mid to senior

    A Java service and a PHP service both compute an invoice total with string(sum(//line/amount)). For a file whose amounts are 0.1 and 0.2, a reconciliation job reports 0.30000000000000004 against 0.3. Which service is wrong, and what do you change so that the totals always agree?

    What it tests
    Knowing that XPath 1.0 numbers are doubles, how the standard turns a number into a string, how libxml2 differs, and why amounts belong in whole units.
    First step
    Neither total is wrong as a number: both engines added the same two doubles. They differ in how they write it. XPath 1.0 asks for as many digits as tell the double apart, which the JDK printed; libxml2 printed at most 15 significant digits. Stop comparing number strings, and sum whole paise instead of rupees.
    Follow-ups

    From the lesson XPath 1.0: paths, axes and predicates

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  3. Fresher to mid

    An XSLT 1.0 stylesheet turns order exports into an HTML table and has worked for a year. A supplier adds a remarks element to each order, and the remarks now appear as loose text above the table. Why, and what fix keeps the next new element out as well?

    What it tests
    Knowing the built-in template rules, where processing starts, and choosing a fix that also holds for elements nobody has added yet.
    First step
    Find where the stylesheet lets the built-in rules run: it has no rule for the root, or it applies templates without a select, so the new element reaches the built-in rule, which copies its text. Give the root rule a select that names exactly the nodes the table needs.
    Follow-ups

    From the lesson XSLT fundamentals: templates and identity

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 2 minutes
  4. Mid to senior

    An XSLT 1.0 report groups 50,000 orders by customer with order[not(@customer = preceding-sibling::order/@customer)] and now takes minutes. Explain where the time goes, and rewrite the grouping so that it scales.

    What it tests
    Estimating the cost of a test on preceding nodes, grouping with xsl:key and generate-id(), and knowing what XSLT 2.0 and later offer instead.
    First step
    Count the work first: the test can compare each order with every order before it, up to n(n − 1)/2 comparisons, about 1.25 billion for 50,000 orders. Then declare a key on @customer and keep the order whose generate-id() equals that of the first order its key returns.
    Follow-ups

    From the lesson XSLT fundamentals: templates and identity

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 2 minutes

All Data Formats (JSON, YAML, TOML, XML, CSV) interview questions · The Data Formats (JSON, YAML, TOML, XML, CSV) track

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.