Program: 63975
Project: 63975
Buggy source:
/** 
 * Handle the marshal operation for this NodeValue.  Each of the target object's primary key values that are mapped to the collection mapping's fields  (in the XMLCollectionReferenceMapping's source-target key field association list) are retrieved and written out. 
 */
public boolean marshal(XPathFragment xPathFragment,MarshalRecord marshalRecord,Object object,CoreAbstractSession session,NamespaceResolver namespaceResolver){
  if (this.xmlCollectionReferenceMapping.isReadOnly()) {
    return false;
  }
  CoreContainerPolicy cp=xmlCollectionReferenceMapping.getContainerPolicy();
  Object collection=xmlCollectionReferenceMapping.getAttributeAccessor().getAttributeValueFromObject(object);
  if (collection == null) {
    return false;
  }
  Object iterator=cp.iteratorFor(collection);
  if (cp.hasNext(iterator)) {
    XPathFragment groupingFragment=marshalRecord.openStartGroupingElements(namespaceResolver);
    marshalRecord.closeStartGroupingElements(groupingFragment);
  }
 else {
    return marshalRecord.emptyCollection(xPathFragment,namespaceResolver,false);
  }
  Object objectValue;
  if (xmlCollectionReferenceMapping.usesSingleNode()) {
    StringBuilder stringValueStringBuilder=new StringBuilder();
    String newValue;
    QName schemaType;
    while (cp.hasNext(iterator)) {
      objectValue=cp.next(iterator,session);
      Object fieldValue=xmlCollectionReferenceMapping.buildFieldValue(objectValue,xmlField,session);
      if (fieldValue == null) {
        if (null != objectValue) {
          Field fkField=(Field)xmlCollectionReferenceMapping.getSourceToTargetKeyFieldAssociations().get(xmlField);
          fieldValue=marshalRecord.getMarshaller().getContext().getValueByXPath(objectValue,fkField.getXPath(),fkField.getNamespaceResolver(),Object.class);
        }
        if (null == fieldValue) {
          break;
        }
      }
      schemaType=xmlField.getSchemaTypeForValue(fieldValue,session);
      newValue=marshalRecord.getValueToWrite(schemaType,fieldValue,(XMLConversionManager)session.getDatasourcePlatform().getConversionManager());
      if (newValue != null) {
        stringValueStringBuilder.append(newValue);
        if (cp.hasNext(iterator)) {
          stringValueStringBuilder.append(SPACE);
        }
      }
    }
    marshalSingleValue(xPathFragment,marshalRecord,object,stringValueStringBuilder.toString(),session,namespaceResolver,ObjectMarshalContext.getInstance());
  }
 else {
    marshalRecord.startCollection();
    while (cp.hasNext(iterator)) {
      objectValue=cp.next(iterator,session);
      marshalSingleValue(xPathFragment,marshalRecord,object,objectValue,session,namespaceResolver,ObjectMarshalContext.getInstance());
    }
    marshalRecord.endCollection();
  }
  return true;
}

Fixed version (ground truth):
/** 
 * Handle the marshal operation for this NodeValue.  Each of the target object's primary key values that are mapped to the collection mapping's fields  (in the XMLCollectionReferenceMapping's source-target key field association list) are retrieved and written out. 
 */
public boolean marshal(XPathFragment xPathFragment,MarshalRecord marshalRecord,Object object,CoreAbstractSession session,NamespaceResolver namespaceResolver){
  if (this.xmlCollectionReferenceMapping.isReadOnly()) {
    return false;
  }
  CoreContainerPolicy cp=xmlCollectionReferenceMapping.getContainerPolicy();
  Object collection=xmlCollectionReferenceMapping.getAttributeAccessor().getAttributeValueFromObject(object);
  if (collection == null) {
    return false;
  }
  Object iterator=cp.iteratorFor(collection);
  if (cp.hasNext(iterator)) {
    XPathFragment groupingFragment=marshalRecord.openStartGroupingElements(namespaceResolver);
    marshalRecord.closeStartGroupingElements(groupingFragment);
  }
 else {
    return marshalRecord.emptyCollection(xPathFragment,namespaceResolver,false);
  }
  Object objectValue;
  if (xmlCollectionReferenceMapping.usesSingleNode()) {
    StringBuilder stringValueStringBuilder=new StringBuilder();
    String newValue;
    QName schemaType;
    while (cp.hasNext(iterator)) {
      objectValue=cp.next(iterator,session);
      Object fieldValue=xmlCollectionReferenceMapping.buildFieldValue(objectValue,xmlField,session);
      if (fieldValue == null) {
        if (null != objectValue) {
          Field fkField=(Field)xmlCollectionReferenceMapping.getSourceToTargetKeyFieldAssociations().get(xmlField);
          fieldValue=marshalRecord.getMarshaller().getContext().getValueByXPath(objectValue,fkField.getXPath(),fkField.getNamespaceResolver(),Object.class);
        }
        if (null == fieldValue) {
          break;
        }
      }
      schemaType=xmlField.getSchemaTypeForValue(fieldValue,session);
      newValue=marshalRecord.getValueToWrite(schemaType,fieldValue,(ConversionManager)session.getDatasourcePlatform().getConversionManager());
      if (newValue != null) {
        stringValueStringBuilder.append(newValue);
        if (cp.hasNext(iterator)) {
          stringValueStringBuilder.append(SPACE);
        }
      }
    }
    marshalSingleValue(xPathFragment,marshalRecord,object,stringValueStringBuilder.toString(),session,namespaceResolver,ObjectMarshalContext.getInstance());
  }
 else {
    marshalRecord.startCollection();
    while (cp.hasNext(iterator)) {
      objectValue=cp.next(iterator,session);
      marshalSingleValue(xPathFragment,marshalRecord,object,objectValue,session,namespaceResolver,ObjectMarshalContext.getInstance());
    }
    marshalRecord.endCollection();
  }
  return true;
}