public function build()
    {
        $prevState = libxml_use_internal_errors(true);

        try {
            $doc = $this->buildDocument();

            if (!empty($this->schema) && !$doc->schemaValidate($this->schema)) {
                throw ReportDefinitionException::schemaValidation(libxml_get_errors());
            }

            if (false === $xml = $doc->saveXML()) {
                throw ReportDefinitionException::compileError(libxml_get_errors());
            }

            return $xml;
        } finally {
            libxml_clear_errors();
            libxml_use_internal_errors($prevState);
        }
    }