public boolean marshal(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, AbstractSession session, NamespaceResolver namespaceResolver) {
XPathFragment groupingFragment = marshalRecord.openStartGroupingElements(namespaceResolver);
if (marshalRecord.getLeafElementType() != null) {
XMLDescriptor xmlDescriptor = (XMLDescriptor) session.getDescriptor(object.getClass());
XMLSchemaReference xmlRef = xmlDescriptor.getSchemaReference();
// only interested in global COMPLEX_TYPE
if (xmlRef.getType() == 1 && xmlRef.isGlobalDefinition()) {
QName ctxQName = xmlRef.getSchemaContextAsQName(xmlDescriptor.getNamespaceResolver());
if (ctxQName.equals(marshalRecord.getLeafElementType())) {
// don't write out xsi:type attribute
marshalRecord.closeStartGroupingElements(groupingFragment);
} else {
// write out the target descriptor's schema context as
// xsi:type
XMLField xmlField = (XMLField) getInheritancePolicy().getClassIndicatorField();
if (xmlField.getLastXPathFragment().isAttribute()) {
marshalRecord.put(xmlField, xmlRef.getSchemaContext().substring(1));
marshalRecord.closeStartGroupingElements(groupingFragment);
} else {
marshalRecord.closeStartGroupingElements(groupingFragment);
marshalRecord.put(xmlField, xmlRef.getSchemaContext().substring(1));
}
}
return true;
}