<?php
function libxml_display_error_brukld($error)
{
	$return = '<br/>' . "\n";

	switch ($error->level) {
	case LIBXML_ERR_WARNING:
		$return .= '<b>Warning ' . $error->code . '</b>: ';
		break;
	case LIBXML_ERR_ERROR:
		$return .= '<b>Error ' . $error->code . '</b>: ';
		break;
	case LIBXML_ERR_FATAL:
		$return .= '<b>Fatal Error ' . $error->code . '</b>: ';
		break;
	}

	$return .= trim($error->message);
	$return .= ' on line <b>' . $error->line . '</b>' . "\n";
	return $return;
}

function libxml_display_error_nohtml_brukld($error)
{
	$return = "\n";

	switch ($error->level) {
	case LIBXML_ERR_WARNING:
		$return .= 'Warning ' . $error->code . ': ';
		break;
	case LIBXML_ERR_ERROR:
		$return .= 'Error ' . $error->code . ': ';
		break;
	case LIBXML_ERR_FATAL:
		$return .= 'Fatal Error ' . $error->code . ': ';
		break;
	}

	$return .= trim($error->message);
	$return .= ' on line ' . $error->line . "\n";
	return $return;
}

function libxml_display_errors_brukld($flaghtml = true)
{
	$result = '';
	$errors = libxml_get_errors();

	foreach ($errors as $error) {
		if ($flaghtml) {
			$result .= libxml_display_error_brukld($error);
		}
		else {
			$result .= libxml_display_error_nohtml_brukld($error);
		}
	}

	libxml_clear_errors();
	return $result;
}

function validate_xml_against_schema_brukld($xmlcontent)
{
	$validation = NULL;
	$file = NULL;
	libxml_use_internal_errors(true);
	$xml = new DOMDocument();
	$xml->loadXML($xmlcontent);
	$file = 'SAP-Compliance-Report.xsd';

	if (!$xml->schemaValidate(__DIR__ . '/XSD/BRUKLD-10.2.21.0.0/Templates/' . $file)) {
		$validation .= '***XML schema validation failed*** ' . PHP_EOL;
		$validation .= htmlentities(libxml_display_errors_brukld(false));
	}

	return $validation;
}

?>