Scrivio Interview library
DesignIntermediate7 min read · updated 12 November 2026

You must produce XML for a partner whose schema uses namespaces, a fixed element order and optional elements. Which JAXB annotations control this, and how do you keep your domain model clean?

@XmlType(propOrder = {...}) fixes the order of child elements, @XmlSchema in package-info.java sets the default namespace, elementFormDefault and prefixes through @XmlNs, and @XmlElement(namespace = ...) places individual elements in other namespaces. Null values are omitted unless an element is marked nillable = true, which writes xsi:nil. To keep the domain model clean, I would put all of this on separate binding classes, generated from the partner's XSD with xjc or written by hand, map them to domain objects, and test the output against the partner's schema.

Why this question is asked

Partner XML formats are usually strict. A bank or a government gateway publishes an XSD, an XML Schema Definition, that fixes namespaces, element order and which elements may be missing, and it rejects documents that do not match. Interviewers ask this design question because getting the annotations right is only half of the job; the other half is keeping a partner's format from leaking into our own domain classes. In this answer, we will recap namespaces and why order matters, see which annotations control each requirement, decide where those annotations should live, and test the result against the schema.

1A Quick Recap of Namespaces and Schema Rules

A namespace is a URI that qualifies element names so that two vocabularies can use the same name without a clash, for example an order element of our partner and an order element of another standard. In the XML text, a namespace is usually represented by a short prefix declared with xmlns:p="...", but the prefix is only a local abbreviation: what matters to every parser is the URI.

An XSD often declares its elements inside a sequence, which means they must appear in exactly the listed order. It may also say elementFormDefault="qualified", which means child elements belong to the schema's namespace too, not just the root. And it marks elements as optional with minOccurs="0" or as allowed to be explicitly empty with nillable="true". Each of these rules has a matching JAXB annotation.

2Setting the Namespace for a Whole Package

The namespace that applies to most of our elements is declared once, in a file named package-info.java in the package of the binding classes. That file holds annotations for the package itself.

Java
// src/main/java/com/acme/partner/bank/xml/package-info.java
@XmlSchema(
namespace = "urn:bank:payments:v2",
elementFormDefault = XmlNsForm.QUALIFIED,
xmlns = {
@XmlNs(prefix = "pay", namespaceURI = "urn:bank:payments:v2"),
@XmlNs(prefix = "cmn", namespaceURI = "urn:bank:common:v1")
})
@XmlAccessorType(XmlAccessType.FIELD)
package com.acme.partner.bank.xml;
import jakarta.xml.bind.annotation.*;

namespace sets the default namespace for every element in the package, and elementFormDefault = QUALIFIED applies it to child elements too, matching the partner's schema. Without QUALIFIED, only the root would be in the namespace and children would be in no namespace, which a strict partner rejects. The @XmlNs entries ask the runtime to use these prefixes, which makes output readable and matches the partner's samples, although a correct parser does not care about prefixes.

3Fixing Order, Mixing Namespaces and Handling Optional Elements

Now the class itself. @XmlType(propOrder) lists the Java property names in the order the elements must appear. Without it, the order depends on the runtime and on how fields are discovered, which is not something to rely on. Every mapped property must be in the list, or context creation fails, which is a useful safety net when someone adds a field.

Java
@XmlRootElement(name = "Payment")
@XmlType(propOrder = {"reference", "amount", "debtor", "remittanceInfo", "purpose"})
public class PaymentXml {
@XmlElement(name = "Ref", required = true)
private String reference;
@XmlElement(name = "Amt", required = true)
private AmountXml amount;
@XmlElement(name = "Party", namespace = "urn:bank:common:v1") // from the common namespace
private PartyXml debtor;
@XmlElement(name = "RmtInf") // optional: omitted when null
private String remittanceInfo;
@XmlElement(name = "Purp", nillable = true) // nillable: written as xsi:nil when null
private String purpose;
}
// <pay:Payment xmlns:pay="urn:bank:payments:v2" xmlns:cmn="urn:bank:common:v1"
// xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
// <pay:Ref>P-1001</pay:Ref>
// <pay:Amt Ccy="EUR">12.50</pay:Amt>
// <cmn:Party>...</cmn:Party>
// <pay:Purp xsi:nil="true"/>
// </pay:Payment>
Partner rule and the JAXB annotation for it
Schema ruleJAXB annotation
targetNamespace for the document@XmlSchema(namespace) in package-info.java
elementFormDefault="qualified"@XmlSchema(elementFormDefault = QUALIFIED)
preferred prefixes@XmlNs inside @XmlSchema
element from another namespace@XmlElement(namespace = "...")
xs:sequence order@XmlType(propOrder = {...})
minOccurs="0"leave null: element omitted
nillable="true"@XmlElement(nillable = true): xsi:nil when null

Notice the two different ways of being empty. remittanceInfo is null and simply does not appear, which matches minOccurs="0" in the schema. purpose is also null, but because it is nillable, JAXB writes the element with xsi:nil="true", which tells the partner that we know the value and it is explicitly empty. Some partner schemas require one and forbid the other, so we read the XSD carefully for each field.

The debtor element uses @XmlElement(namespace = ...) to come from the common namespace, so it is written with the cmn prefix even though the rest of the class uses the package default. The same attribute exists on @XmlAttribute and @XmlRootElement.

4Keeping the Domain Model Clean

All of these annotations describe the partner's format, not our business. If we put them on our domain entities, three problems follow. The entity's field names and structure are pulled toward the partner's choices, such as Amt and RmtInf. A second partner with a different format cannot be supported on the same class. And when the partner releases a new schema version, our core domain changes with it.

So the binding classes are separate data transfer objects, DTOs, that exist only to match the XML. When the partner publishes an XSD, we generate them with xjc, JAXB's schema compiler, as part of the build, and never edit them by hand. When there is no schema or it is small, hand-written DTOs like PaymentXml are fine. Either way, a mapper converts between DTOs and domain objects, and it is the only code that knows both.

Java
// Domain: our language, no XML annotations
public record Payment(String reference, Money amount, Party debtor, Optional<String> note) {}
// Mapper: the only class that knows both sides
final class PaymentXmlMapper {
PaymentXml toXml(Payment p) {
PaymentXml x = new PaymentXml();
x.setReference(p.reference());
x.setAmount(AmountXml.of(p.amount()));
x.setDebtor(PartyXml.of(p.debtor()));
x.setRemittanceInfo(p.note().orElse(null)); // omitted when absent
return x;
}
}

The extra mapping code is the price, and it is a small one: it lives in one integration module, it is easy to test, and it means a schema change touches the DTOs and the mapper only. Mapping libraries such as MapStruct can generate most of it.

5Testing Against the Partner Schema

Annotations are easy to get subtly wrong: a missing namespace, a wrong order, an optional element written as nil. The partner's XSD can check all of that for us. In a unit test we compile the schema once, set it on the marshaller, and marshal sample objects. Any violation fails the test with the exact line and rule. We also unmarshal the partner's own sample messages with the schema set, so we know we can read what they send.

Java
Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
.newSchema(getClass().getResource("/xsd/bank-payments-v2.xsd"));
Marshaller m = CTX.createMarshaller();
m.setSchema(schema); // validate while writing
m.marshal(samplePayment(), new StringWriter());
// passes, or MarshalException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'pay:Purp' ...

6Follow-up questions

What if propOrder is wrong or incomplete?

If it names a property that does not exist or leaves out a mapped property, JAXBContext creation fails at startup with an IllegalAnnotationsException, which is good: the error appears immediately. If it is complete but in the wrong order, only schema validation will catch it, which is why the schema test matters.

How do I write an attribute in another namespace, such as xml:lang?

Use @XmlAttribute(name = "lang", namespace = "http://www.w3.org/XML/1998/namespace"). The prefix xml is predefined, so JAXB writes xml:lang automatically.

Should generated classes be committed to the repository?

Usually not. Generating them in the build from a versioned copy of the XSD makes the schema the source of truth and prevents hand edits. Committing them can make sense when the generator is unstable or slow, but then the build should check they are up to date.

7Conclusion

In this answer, we saw how a strict partner schema maps onto JAXB: @XmlSchema in package-info.java sets the default namespace, qualified element form and prefixes, @XmlElement(namespace) brings in elements from other namespaces, @XmlType(propOrder) fixes the order, and nillable controls whether a null becomes xsi:nil or simply disappears. We kept those annotations on separate binding DTOs, generated by xjc or hand-written, with a mapper to our domain, and tested everything against the partner's XSD.

What to say in the interviewAbout 80 seconds aloud

Open with

For the namespaces I would use package-info.java with @XmlSchema: the partner's namespace as the default, elementFormDefault set to QUALIFIED so child elements are in the namespace too, and @XmlNs entries for the prefixes their samples use, even though parsers only care about the URI.

Then, if they want more

Elements from another namespace get @XmlElement with an explicit namespace. For the fixed order I use @XmlType with propOrder listing every property in sequence order; JAXB fails at startup if the list is incomplete, which is a nice safety net.

Optional elements are just left null, and JAXB omits them; if the schema wants an explicit empty marker, I mark the element nillable true and JAXB writes xsi:nil. To keep the domain clean, I would not put any of this on domain entities.

The binding classes are separate DTOs, generated from the partner's XSD with xjc in the build when they publish one, or hand-written when it is small, and a mapper converts between them and our domain records. That way a new schema version or a second partner changes only the integration module.

Finally, I would test by setting the partner's compiled Schema on the marshaller in unit tests and by unmarshalling their sample messages with validation on.

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 "You must produce XML for a partner whose schema uses namespaces, a fixed element order...": https://scrivio-interview-library.netlify.app/java/jaxb/4297d63f5f/