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.
Warm-up questions
Short questions with short answers, each answered in full.
-
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
-
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.
-
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
-
- Why not strip apostrophes from the input?
- What else could a pasted name do besides cause an error?
- How do you stop the next developer from bringing the bug back?
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.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
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
-
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
-
- Which engine follows the XPath 1.0 standard here?
- How do you compute the total exactly in XPath 1.0?
- What would moving to XPath 3.1 change?
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
-
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
-
- Why did nothing go wrong for a year?
- Would an empty rule for remarks be enough?
- How do you catch this before a supplier changes the format?
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
-
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
-
- How do you check that the new version gives the same groups?
- What changes when the orders come from several documents?
- How does XSLT 3.0 write the same grouping?
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