Quick revision
The short answer
Each option fits a different situation. XSLT keeps mappings declarative and changeable without recompiling and handles reordering and grouping well, but few developers are fluent in it and debugging is harder. Jakarta XML Binding gives typed Java classes from annotations or the partners' XSDs and suits stable schemas, while hand-written StAX writers give full control and streaming for large volumes at the cost of more code. For three outputs from one order model, I would usually build an internal model once and use binding for the XML formats, switching to StAX for any feed too large to hold in memory, and validate every output against the partner XSD in tests.
What to remember
In this answer, we compared XSLT, Jakarta XML Binding and StAX writers for producing two XML formats and a CSV. XSLT is declarative and changeable without recompiling and great at reordering and grouping, but needs rare skills and is harder to debug; binding gives type safety from XSDs for stable schemas; StAX gives full control and flat memory for large volumes at the cost of more code. Mapping from one canonical model keeps outputs consistent, a CSV library handles CSV, XSLT 2.0 and 3.0 need Saxon, and every output is validated against the partner XSD in tests.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Each option fits a different situation. XSLT keeps mappings declarative and changeable without recompiling and handles reordering and grouping well, but few developers are fluent in it and debugging is harder. Jakarta XML Binding gives typed Java classes from annotations or the partners' XSDs and suits stable schemas, while hand-written StAX writers give full control and streaming for large volumes at the cost of more code. For three outputs from one order model, I would usually build an internal model once and use binding for the XML formats, switching to StAX for any feed too large to hold in memory, and validate every output against the partner XSD in tests.
Why this question is asked
Producing the same data in several formats is a very common integration task: one partner wants their schema, another wants a different schema, and a third wants a CSV for a spreadsheet. The technology choice shapes how quickly we can change a mapping, who can change it, and whether a large export fits in memory. Interviewers ask this design question to hear trade-offs, not a single right answer. In this answer, we will recap the three options, see what each one costs, design a structure that keeps the three outputs consistent, pick an approach for typical numbers, and finally make the mappings testable, which is also the follow-up question.
1A Quick Recap of the Three Options
XSLT is a language for transforming XML documents. We write a stylesheet of templates that match parts of the input and produce output, which can be XML, text or CSV. The JDK includes an XSLT 1.0 processor; XSLT 2.0 and 3.0, with grouping, functions, regular expressions and better text output, need Saxon.
Jakarta XML Binding, the successor of JAXB in the jakarta.xml.bind package with an implementation such as Glassfish's jaxb-runtime, maps Java classes to XML. We generate classes from each partner's XSD with xjc, fill them from our order objects in Java, and let a Marshaller write the XML. StAX writers, XMLStreamWriter, let us write elements and attributes one by one directly to the output stream, so nothing is built in memory.
2What Each Option Costs
Each option trades flexibility, safety and performance differently.
| Aspect | XSLT | Jakarta XML Binding | StAX writer |
|---|---|---|---|
| Change a mapping | edit stylesheet, no recompile | Java change and release | Java change and release |
| Type safety | none, errors at runtime | compile-time, generated classes | none for names, manual |
| Reorder and group | very good (XSLT 2.0+) | in Java code | in Java code |
| Memory for large volume | full tree unless streaming XSLT 3.0 | whole object graph | flat, streams |
| Skills on the team | rare | common | common |
| Debugging | harder, special tools | normal Java debugger | normal Java debugger |
| CSV output | possible with text method | not suitable, use a CSV library | not suitable, use a CSV library |
XSLT's strength is that mappings become data rather than code: an integration team can change a stylesheet without a Java release, and grouping orders by customer or reordering fields is concise. Its weakness is people. Few developers read XSLT fluently, errors show up at runtime, and debugging a template that matches the wrong node needs specialised tools. Jakarta XML Binding is the opposite: every field is a typed Java property, the compiler catches a renamed element after regenerating from a new XSD, and any Java developer can follow the code, but every mapping change needs a build and release, and it holds the whole object graph in memory. StAX writers stream with flat memory and give full control of the output, but we write and maintain more code, and nothing checks that our element names match the schema except tests.
3A Structure That Keeps the Outputs Consistent
Whatever tool we pick, the most important design decision is to map from one internal model. We load orders into our own canonical Order model once, with business rules applied there (totals, tax, status), and each output format is a thin mapper from that model. Then a rule change happens in one place, and the three formats cannot drift apart.
- Order sourcesdatabase, events
- Canonical Order modelbusiness rules applied once
- Exporterspartner A binding, partner B StAX, CSV library
- ValidationXSD for XML outputs, column checks for CSV
- DeliverySFTP, HTTP, object storage
5 layers, from the top down. Step through them one at a time.
Notice that the CSV gets its own tool. XSLT can produce CSV, but quoting fields that contain commas, quotes or newlines correctly is easy to get wrong by hand, and a CSV library already handles it.
4What I Would Choose and Why
Suppose partner A receives about 500 orders per request, partner B a nightly file of 2 million orders, and the business changes the mappings two or three times a year. Then I would use Jakarta XML Binding for partner A: the volume is small, schemas are stable, and type safety plus generated classes make regular schema upgrades safe. For partner B, 2 million orders as an object graph would need gigabytes, so I would write it with XMLStreamWriter while streaming orders from the database, using the generated classes' element names as a reference and validating in tests. A hybrid also works: marshal one generated object per order into the stream with Marshaller.JAXB_FRAGMENT set to true, keeping type safety and flat memory.
I would choose XSLT when mappings change often, perhaps monthly, and a team that knows XSLT owns them, or when the input is already XML and the job is mostly restructuring. In that case I would use Saxon for XSLT 3.0 rather than the JDK's 1.0 processor.
5Keeping the Mapping Testable
The follow-up question is how to keep this testable, and the answer is the same for every option: fixed inputs, expected outputs and schema validation. We keep a set of sample orders covering each case (a single item, many items, a refund, missing optional fields, characters such as & and U+00E9 in names) and for each exporter an expected output file. The test runs the exporter, compares the result with the expected file in a way that ignores insignificant whitespace, and validates every XML output against the partner's XSD.
For XSLT the same tests apply, and XSpec adds unit tests for individual templates. For StAX exporters the XSD validation is particularly valuable, because it is the only check that element names and order match the schema. A test for partner B also runs a large generated input with a small heap, to prove the exporter really streams.
6Follow-up questions
How do you keep the mapping testable?
By separating mapping from transport and testing it with fixed inputs and expected outputs. Each exporter takes orders and writes to an OutputStream, so tests need no network. We keep sample orders for every business case, compare outputs with expected files, and validate every XML output against the partner's XSD. When a partner sends a new XSD version, the tests show exactly what changes.
Is XSLT 1.0 in the JDK good enough?
For simple restructuring, yes. It lacks grouping, regular expressions, user functions in XSLT itself and good text output, so anything beyond simple mappings becomes awkward. Saxon's free edition supports XSLT 3.0 for most needs; streaming needs a commercial edition, so check the Saxon editions.
Would you let partners' XSDs shape our internal model?
No. Generated classes belong at the edge. If the internal model followed partner A's schema, partner B's mapping and every future format would depend on it, and every partner schema change would ripple into business code.
7Conclusion
In this answer, we compared XSLT, Jakarta XML Binding and StAX writers for producing two XML formats and a CSV. XSLT is declarative and changeable without recompiling and great at reordering and grouping, but needs rare skills and is harder to debug; binding gives type safety from XSDs for stable schemas; StAX gives full control and flat memory for large volumes at the cost of more code. Mapping from one canonical model keeps outputs consistent, a CSV library handles CSV, XSLT 2.0 and 3.0 need Saxon, and every output is validated against the partner XSD in tests.
Open with
I would first make sure all three outputs map from one internal order model, with business rules applied once, so the formats cannot drift. Then I pick the tool per format.
Then, if they want more
XSLT is attractive when mappings change often, because a stylesheet can be changed without a Java release, and it is very good at reordering and grouping, but few developers read it fluently, errors appear at runtime and debugging is harder. Also the JDK only has XSLT 1.0; for 2.0 or 3.0 we need Saxon.
Jakarta XML Binding, with classes generated from each partner's XSD, gives me compile-time safety and plain Java code, which is ideal for stable schemas and moderate volumes. StAX with XMLStreamWriter gives full control and flat memory for huge feeds, but it is more code and nothing checks names except tests.
So for a small request-based partner I would use binding, for a nightly multi-million order file StAX, or binding per order as a fragment into a StAX stream, and for the CSV a proper CSV library rather than XSLT or string building.
For testability, every exporter writes to an OutputStream, and tests run sample orders for each business case, compare with expected files and validate each XML output against the partner's XSD. A large-input test with a small heap proves the streaming exporter really streams.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Partners need the same order data in two XML formats and a CSV. Would you use XSLT, a...": https://scrivio-interview-library.netlify.app/java/xml-processing/aebe10ee22/