Version: current

     1 <?php
     2
     3 //Валидатор XML
     4 class XMLValidate {
     5
     6     protected $xmlFile;
     7     protected $schema;
     8     function __construct($xmlFile, $schema) {
     9         $this->xmlFile = $xmlFile;
    10         $this->schema = $schema;
    11     }
    12
    13     /*
    14      * Проверка валидности XML-файла по схеме
    15      *
    16      * @return string/array
    17      */
    18     function validate() {
    19         // Enable user error handling
    20         libxml_use_internal_errors(true);
    21
    22         $xml = new DOMDocument();
    23         $xml->load($this->xmlFile);
    24
    25         $xsdPath = $this->schema;
    26
    27         if (!$xml->schemaValidate($xsdPath)) {
    28             $errors = libxml_get_errors();
    29             $errorMessage = array();
    30             foreach ($errors as $error) {
    31               $errorMessage[] = $this->libxml_display_error($error);
    32
    33             }
    34             libxml_clear_errors();
    35             return $errorMessage;
    36         } else {
    37             return 'validated';
    38         }
    39     }
    40
    41     /**
    42      * libXML error catcher.
    43      *
    44      * @param $error
    45      * @return string
    46      */
    47     function libxml_display_error($error) {
    48         $return = "";
    49         switch ($error->level) {
    50             case LIBXML_ERR_WARNING:
    51                 $return .= _t('XMLValidate.Error', 'Error') . " $error->code: "; //Warning
    52                 break;
    53             case LIBXML_ERR_ERROR:
    54                 $return .= _t('XMLValidate.Error', 'Error') . " $error->code: ";
    55                 break;
    56             case LIBXML_ERR_FATAL:
    57                 $return .= _t('XMLValidate.FatalError', 'Fatal Error') . " $error->code: ";
    58                 break;
    59         }
    60         $return .= trim($error->message);
    61         /*
    62         if ($error->file) {
    63             $return .=    " in <b>$error->file</b>";
    64         }
    65         */
    66         $return .= _t('XMLValidate.AtLine', 'At line') . " $error->line";
    67
    68         return $return;
    69     }
    70
    71 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -