/** * That validate function takes an XSD to valid against `$this->_xml` * returning boolean. Optionally, a second parameter `$xml` can be * passed that will be used instead of `$this->_xml`. * * @since Symphony 2.3 * @param string $xsd * The XSD to validate `$this->_xml` against * @param string $xml (optional) * If provided, this function will use this `$xml` instead of * `$this->_xml`. * @return boolean * Returns true if the `$xml` validates against `$xsd`, false otherwise. * If false is returned, the errors can be obtained with `XSLTProcess->getErrors()` */ public function validate($xsd, $xml = null) { if (is_null($xml) && !is_null($this->_xml)) { $xml = $this->_xml; } if (is_null($xsd) || is_null($xml)) { return false; } // Create instances of the DomDocument class $xmlDoc = new DomDocument(); // Set up error handling if (function_exists('ini_set')) { $ehOLD = ini_set('html_errors', false); } // Load the xml document set_error_handler(array($this, 'trapXMLError')); $elOLD = libxml_disable_entity_loader(true); $xmlDoc->loadXML($xml, LIBXML_NONET | LIBXML_DTDLOAD | LIBXML_DTDATTR | defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0); libxml_disable_entity_loader($elOLD); // Must restore the error handler to avoid problems restore_error_handler(); // Validate the XML against the XSD set_error_handler(array($this, 'trapXSDError')); $result = $xmlDoc->schemaValidateSource($xsd); // Restore error handling if (function_exists('ini_set') && isset($ehOLD)) { ini_set('html_errors', $ehOLD); } // Must restore the error handler to avoid problems restore_error_handler(); return $result; }
/** * Validates the current document against an XML schema, * optionally validates embedded Schematron rules too. * * \param string $source * Source of the XML schema to use for validation. * * \param bool $schematron * (optional) Whether embedded Schematron rules * should be validated too (\b true) or not (\b false). * The default is to also do $schematron validation. * * \retval bool * \b true if the document validates, * \b false otherwise. */ public function schemaValidateSource($source, $schematron = true) { $success = parent::schemaValidateSource($source); return $this->schematronValidation('string', $source, 'XSD', $success, $schematron); }
$doc = new DomDocument(); $doc->loadXML(stripslashes($_POST['xml'])); $xmlout = htmlspecialchars(stripslashes($_POST['xml'])); } if ((!isset($doc) || isset($_REQUEST['itype']) && $_REQUEST['itype'] == 'url') && preg_match('%^https?://%', $_REQUEST['uri'])) { $xmlFile = $_REQUEST['uri']; $doc = new DomDocument(); $xmlout = file_get_contents($xmlFile); $doc->loadXML($xmlout); //$doc->load( $xmlFile ); $xmlout = htmlspecialchars($xmlout); } if (isset($doc)) { $xsd = file_get_contents($xsdURI); set_error_handler('err_handler'); if ($doc->schemaValidateSource($xsd)) { $result .= "XML is valid."; } else { $result .= "XML is <em>NOT</em> valid."; } } else { $result = "XML File specified is not valid."; } echo '<div class="result">' . $result . '</div>'; } if (!isset($xmlout)) { $xmlout = ''; } ?> <div>Schema used for validation is: <a href="<?php
private function schemaValidate(\DomDocument $dom, $xsd) { try { $dom->schemaValidateSource($xsd); $this->throwError(); } catch (\DOMException $e) { throw new \RuntimeException($e->getMessage()); } }