XML interview question · Most asked
How does XSLT work? Explain templates and apply-templates.
Summary
An XSLT stylesheet is a set of template rules. The processor starts at the root, finds the best-matching template, and the template outputs content and calls xsl:apply-templates to process selected nodes with their own matching templates. Built-in rules copy text and recurse when no template matches.
Longer Answer
Push style (apply-templates, one template per node type) is more maintainable than one big xsl:for-each. The identity transform plus overrides is the standard way to make small changes to a document.
Key Points
- Template rules match patterns.
- apply-templates recurses.
- Built-in rules.
- Identity transform pattern.
Deep dive: XML Tutorial: XSLT Basics
Explanation
What interviewers look for
Push vs pull and the identity transform.
More most-asked XML questions
- What's the difference between well-formed and valid XML?
- Explain XML namespaces and the default namespace.
- Compare DOM, SAX and StAX (or pull parsing).
- Your XPath query returns nothing, but the elements are clearly there. What's the likely cause?
- What is an XXE attack and how do you prevent it?
- What is XML and where is it still used today?
- Which characters must be escaped in XML, and what is CDATA?
- Compare DTD, XML Schema (XSD), RELAX NG and Schematron.