public function format($id, $recordObj)
    {
        if (!isset($recordObj->metadata)) {
            throw new \Exception("Unexpected missing record metadata.");
        }

        $raw = trim($recordObj->metadata->asXML());

        // Extract the actual metadata from inside the <metadata></metadata> tags;
        // there is probably a cleaner way to do this, but this simple method avoids
        // the complexity of dealing with namespaces in SimpleXML.
        //
        // We should also apply global search and replace at this time, if
        // applicable.
        $record = $this->performGlobalReplace(
            preg_replace('/(^<metadata[^\>]*>)|(<\/metadata>$)/m', '', $raw)
        );

        // Collect attributes (for proper namespace resolution):
        $metadataAttributes = $this->extractMetadataAttributes($raw, $record);

        // If we are supposed to inject any values, do so now inside the first
        // tag of the file:
        $insert = $this->getIdAdditions($id)
            . $this->getHeaderAdditions($recordObj->header);
        $xml = !empty($insert)
            ? preg_replace('/>/', '>' . $insert, $record, 1) : $record;

        // Build the final record:
        return trim(
            $this->fixNamespaces(
                $xml, $recordObj->getDocNamespaces(), $metadataAttributes
            )
        );
    }