public static function load($file) { $document = new DOMDocument(); $document->load($file); $document->xinclude(); return $document->saveXML(); }
function loadRecordHML($filename) { global $recID; $dom = new DOMDocument(); $dom->load($filename); $dom->xinclude(); return $dom->saveXML(); }
function main() { $uri = realpath(__DIR__ . '/xinclude-1.xml'); $xml = file_get_contents($uri); $doc = new DOMDocument(); $doc->loadXML($xml); $doc->documentURI = $uri; $doc->xinclude(); var_dump($doc->saveXML()); }
static function load($filename) { $document = new \DOMDocument(); if ($document->load($filename) !== false) { $document->xinclude(); return new document($document); } else { return null; } }
/** * Load an $actual document into a DOMDocument. This is called * from the selector assertions. * * If $actual is already a DOMDocument, it is returned with * no changes. Otherwise, $actual is loaded into a new DOMDocument * as either HTML or XML, depending on the value of $isHtml. If $isHtml is * false and $xinclude is true, xinclude is performed on the loaded * DOMDocument. * * Note: prior to PHPUnit 3.3.0, this method loaded a file and * not a string as it currently does. To load a file into a * DOMDocument, use loadFile() instead. * * @param string|DOMDocument $actual * @param bool $isHtml * @param string $filename * @param bool $xinclude * @param bool $strict * * @return DOMDocument * * @since Method available since Release 3.3.0 */ public static function load($actual, $isHtml = false, $filename = '', $xinclude = false, $strict = false) { if ($actual instanceof DOMDocument) { return $actual; } if (!is_string($actual)) { throw new PHPUnit_Framework_Exception('Could not load XML from ' . gettype($actual)); } if ($actual === '') { throw new PHPUnit_Framework_Exception('Could not load XML from empty string'); } // Required for XInclude on Windows. if ($xinclude) { $cwd = getcwd(); @chdir(dirname($filename)); } $document = new DOMDocument(); $document->preserveWhiteSpace = false; $internal = libxml_use_internal_errors(true); $message = ''; $reporting = error_reporting(0); if ('' !== $filename) { // Necessary for xinclude $document->documentURI = $filename; } if ($isHtml) { $loaded = $document->loadHTML($actual); } else { $loaded = $document->loadXML($actual); } if (!$isHtml && $xinclude) { $document->xinclude(); } foreach (libxml_get_errors() as $error) { $message .= "\n" . $error->message; } libxml_use_internal_errors($internal); error_reporting($reporting); if ($xinclude) { @chdir($cwd); } if ($loaded === false || $strict && $message !== '') { if ($filename !== '') { throw new PHPUnit_Framework_Exception(sprintf('Could not load "%s".%s', $filename, $message != '' ? "\n" . $message : '')); } else { if ($message === '') { $message = 'Could not load XML for unknown reason'; } throw new PHPUnit_Framework_Exception($message); } } return $document; }
protected function validateConfig($filename = 'simple.xml') { $xml_file = realpath(__DIR__ . '/Fixtures/' . $filename); $this->assertTrue(is_readable($xml_file), 'XML file ' . $xml_file . ' is not readable.'); $schema_file = realpath(__DIR__ . '/../../../../../src/Environaut/Config/Reader/Schema/environaut.xsd'); $this->assertTrue(is_readable($schema_file), 'Schema file ' . $schema_file . ' is not readable.'); libxml_use_internal_errors(true); $dom = new \DOMDocument('1.0', 'UTF-8'); $this->assertTrue($dom->load($xml_file, LIBXML_NOCDATA), 'Could not load XML file ' . $xml_file . ' - Error was: ' . print_r(libxml_get_errors(), true)); $this->assertNotEquals(-1, $dom->xinclude(), 'XInclude resolution failed. Error was: ' . print_r(libxml_get_errors(), true)); $valid = $dom->schemaValidate($schema_file); libxml_use_internal_errors(false); return $valid; }
function loadRecordHTML($recHMLFilename, $styleFilename) { global $recID, $outputFilename; $recHmlDoc = new DOMDocument(); $recHmlDoc->load($recHMLFilename); $recHmlDoc->xinclude(); if (!$styleFilename) { return $recHmlDoc->saveHTMLFile($outputFilename); } $xslDoc = DOMDocument::load($styleFilename); $xslProc = new XSLTProcessor(); $xslProc->importStylesheet($xslDoc); // set up common parameters for stylesheets. // $xslProc->setParameter('','hbaseURL',HEURIST_BASE_URL); // $xslProc->setParameter('','dbName',HEURIST_DBNAME); // $xslProc->setParameter('','dbID',HEURIST_DBID); $xslProc->setParameter('', 'standalone', '1'); $xslProc->transformToURI($recHmlDoc, $outputFilename); }
function process($book = NULL) { if (!empty($book)) { // Load the XML source $xml = new DOMDocument(); $xml->load(SRC_DIR . "/{$book}/book.xml"); // Interpolate all XInclude directives. $xml->xinclude(); // Save it back out. $xml->save(SCRIPT_DIR . "/resolved/{$book}.xml"); } else { // Load the XML source $xml = new DOMDocument(); $xml->load(SRC_DIR . '/set.xml'); // Interpolate all XInclude directives. $xml->xinclude(); // Save it back out. $xml->save(SCRIPT_DIR . '/resolved/set.xml'); } }
/** * Call this function to activate the xml handler * * * @param string the name of the xml file to process * @param string - path of parent. default to root * @return boolean success */ public function process($src, $parent = false) { //load xml file $doc = new DOMDocument('1.0', 'UTF-8'); $doc->load($src); $doc->xinclude(); $flow = Nexista_Flow::singleton('Nexista_Flow'); //import new doc into flow recursively $new = $flow->flowDocument->importNode($doc->documentElement, 1); //append to parent if called for if ($parent) { $res = Nexista_Flow::find($parent); if ($res->length > 0) { $parent = $res->item(0); $parent->appendChild($new); } else { return false; } } else { $flow->root->appendChild($new); } return true; }
function process() { // Load the XML source $xml = new DOMDocument(); $xml->load(SRC_DIR . '/set.xml'); // Interpolate all XInclude directives. $xml->xinclude(); // Load the XSLT stylesheet. $xsl = new DOMDocument(); $xsl->load(DOCBOOK_STYLESHEET_DIR . '/html/docbook.xsl'); // Configure the transformer $proc = new XSLTProcessor(); if (!$proc->hasExsltSupport()) { throw new Exception('EXSLT Support not available.'); } $proc->importStyleSheet($xsl); // attach the xsl rules //file_put_contents('test.html', $proc->transformToXml($xml)); $book = $xml->getElementById('tm'); var_dump($book); var_dump(gettype($book)); file_put_contents('test-book.html', $proc->transformToXml($book)); }
function __construct($xml, $documentURI = false, $flag = self::XML_FILE) { $d = new \DOMDocument(); switch ($flag) { case self::XML_FILE: if (!$d->load($xml)) { throw new \Exception("Failed to load factory XML", 92656); } break; case self::XML_STRING: if (!$d->loadXML($xml)) { throw new \Exception("Failed to load factory XML", 92656); } if ($documentURI) { $d->documentURI = $documentURI; } break; default: throw new \Exception("Invalid construct flag", 95637); } if (-1 === $d->xinclude()) { throw new \Exception("Failed to load factory XML", 92657); } else { if (!($this->simp = simplexml_import_dom($d))) { throw new \Exception("Failed to load factory XML", 92658); } } switch ($tmp = strtolower((string) $this->simp->getName())) { case 'setup': case 'methods': $this->rootEl = $tmp; break; default: throw new \Exception("Unexpected Factory configuration data root element", 17893); } }
function import($filename, $params = array()) { fs::exists($filename) or runtime_error('XSL stylesheet not found: ' . $filename); $xsl = new DOMDocument(); $xsl->load(fs::normalize($filename)); $import = $xsl->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:import'); $href = $xsl->createAttribute('href'); $href->value = dirname(__FILE__) . '/xpath.xsl'; $import->appendChild($href); $xsl->firstChild->insertBefore($import, $xsl->firstChild->firstChild); $xsl->xinclude(); $this->xslt->importStylesheet($xsl); foreach ($this->params as $name => $value) { $this->xslt->removeParameter('', $name); } $this->params = $params; $this->xslt->setParameter('', $params); }
/** * Substitutes XIncludes in a DOMDocument object. * * @param int Bitwise OR of the libxml option constants. * * @return int The number of XIncludes in the document. * * @author Noah Fontes <*****@*****.**> * @since 1.0.0 */ public function xinclude($options = 0) { $luie = libxml_use_internal_errors(true); libxml_clear_errors(); $result = parent::xinclude($options); if (libxml_get_last_error() !== false) { $throw = false; $errors = array(); foreach (libxml_get_errors() as $error) { if ($error->level != LIBXML_ERR_WARNING) { $throw = true; } $errors[] = sprintf('[%s #%d] Line %d: %s', $error->level == LIBXML_ERR_WARNING ? 'Warning' : ($error->level == LIBXML_ERR_ERROR ? 'Error' : 'Fatal'), $error->code, $error->line, $error->message); } libxml_clear_errors(); if ($throw) { libxml_use_internal_errors($luie); throw new DOMException(sprintf('Error%s occurred while resolving XInclude directives: ' . "\n\n%s", count($errors) > 1 ? 's' : '', implode("\n", $errors))); } } libxml_use_internal_errors($luie); // necessary due to a PHP bug, see http://trac.agavi.org/ticket/621 and http://bugs.php.net/bug.php?id=43364 if (version_compare(PHP_VERSION, '5.2.6', '<')) { $documentUri = $this->documentURI; $this->loadXml($this->saveXml()); $this->documentURI = $documentUri; } return $result; }
echo "Loading and parsing {$ac["INPUT_FILENAME"]}... "; flush(); $dom = new DOMDocument(); // realpath() is important: omitting it causes severe performance degradation // and doubled memory usage on Windows. $didLoad = $dom->load(realpath("{$ac['srcdir']}/{$ac["INPUT_FILENAME"]}"), $LIBXML_OPTS); // Check if the XML was simply broken, if so then just bail out if ($didLoad === false) { echo "failed.\n"; print_xml_errors(); errors_are_bad(1); } echo "done.\n"; echo "Validating {$ac["INPUT_FILENAME"]}... "; flush(); $dom->xinclude(); print_xml_errors(); if ($ac['PARTIAL'] != '' && $ac['PARTIAL'] != 'no') { // {{{ $dom->validate(); // we don't care if the validation works or not $node = $dom->getElementById($ac['PARTIAL']); if (!$node) { echo "failed.\n"; echo "Failed to find partial ID in source XML: {$ac['PARTIAL']}\n"; errors_are_bad(1); } if ($node->tagName !== 'book' && $node->tagName !== 'set') { // this node is not normally allowed here, attempt to wrap it // in something else $parents = array();
/** * Load an $actual document into a DOMDocument. This is called * from the selector assertions. * * If $actual is already a DOMDocument, it is returned with * no changes. Otherwise, $actual is loaded into a new DOMDocument * as either HTML or XML, depending on the value of $isHtml. If $isHtml is * false and $xinclude is true, xinclude is performed on the loaded * DOMDocument. * * Note: prior to PHPUnit 3.3.0, this method loaded a file and * not a string as it currently does. To load a file into a * DOMDocument, use loadFile() instead. * * @param string|DOMDocument $actual * @param boolean $isHtml * @param string $filename * @param boolean $xinclude * * @return DOMDocument * @since Method available since Release 3.3.0 * @author Mike Naberezny <*****@*****.**> * @author Derek DeVries <*****@*****.**> * @author Tobias Schlitt <*****@*****.**> */ public static function load($actual, $isHtml = false, $filename = '', $xinclude = false) { if ($actual instanceof DOMDocument) { return $actual; } $document = new DOMDocument(); $internal = libxml_use_internal_errors(true); $message = ''; $reporting = error_reporting(0); if ($isHtml) { $loaded = $document->loadHTML($actual); } else { $loaded = $document->loadXML($actual); } if ('' !== $filename) { // Necessary for xinclude $document->documentURI = $filename; } if (!$isHtml && $xinclude) { $document->xinclude(); } foreach (libxml_get_errors() as $error) { $message .= $error->message; } libxml_use_internal_errors($internal); error_reporting($reporting); if ($loaded === false) { if ($filename != '') { throw new PHPUnit_Framework_Exception(sprintf('Could not load "%s".%s', $filename, $message != '' ? "\n" . $message : '')); } else { throw new PHPUnit_Framework_Exception($message); } } return $document; }
static function load($filename) { $xml = new DOMDocument(); $xml->load($filename) or runtime_error('XML document not found: ' . $filename); $xml->xinclude(); return new xml($xml); }
/** * Return DomDocument object for current xml * * @return \DOMDocument */ public function getDom() { $xmlString = $this->outputMemory(); $dom = new \DOMDocument(); $dom->loadXML($xmlString); if ($this->hasIncludedXml()) { $dom->xinclude(); } return $dom; }
} } } } } } } } if (isset($_POST["law_import"]) && isset($_FILES["law_import_file"])) { $file = $_FILES["law_import_file"]; if (!is_uploaded_file($file['tmp_name'])) { die(T_('Please enter a valid path for the file to upload')); } $doc = new DOMDocument(); $doc->load($file['tmp_name']); $doc->xinclude(); $doc->validate() or die('Malformed XML file'); if ($doc->doctype->publicId != 'DemoWave//law//0.1.0') { die('Not a DemoWave Law file'); } function domNodeIsEmpty($node) { return $node->nodeType == XML_TEXT_NODE && trim($node->data) == ''; } function insertNode($path, $node) { $children = $node->childNodes; $virtual = true; $title = ''; $text = ''; $pos = 0;
* @link http://stackoverflow.com/questions/13865149/speeding-up-xml-schema-validations-of-a-batch-of-xml-files-against-the-same-xml */ $mapping = ['http://www.w3.org/2002/08/xhtml/xhtml1-transitional.xsd' => 'schema/xhtml1-transitional.xsd', 'http://www.w3.org/2001/xml.xsd' => 'schema/xml.xsd']; libxml_set_external_entity_loader(function ($public, $system, $context) use($mapping) { if (is_file($system)) { return $system; } if (isset($mapping[$system])) { return __DIR__ . '/' . $mapping[$system]; } $message = sprintf("Failed to load external entity: Public: %s; System: %s; Context: %s", var_export($public, 1), var_export($system, 1), strtr(var_export($context, 1), [" (\n " => '(', "\n " => '', "\n" => ''])); throw new RuntimeException($message); }); $data_dom = new DOMDocument(); $data_dom->load('test-data.xml'); $data_dom->xinclude(); // Multiple validations using the schemaValidate method. for ($attempt = 1; $attempt <= 3; $attempt++) { $start = time(); echo "schemaValidate: Attempt #{$attempt} returns "; if (!$data_dom->schemaValidate('test-schema.xsd')) { echo "Invalid!"; } else { echo "Valid!"; } $end = time(); echo " in " . ($end - $start) . " seconds.\n"; } // Loading schema into a string. $schema_source = file_get_contents('test-schema.xsd'); // Multiple validations using the schemaValidate method.
/** * Helper method. Load text into the spectified document as XML * @param DOMDocument $doc * @param string $contentfile the file to load * @param string $setEncoding Defaults to true. If true, set the encoding to be that of the docuemnt. Only useful as false for loading root template files * @returns boolean False on failure, */ protected function _loadText($doc, $text, $setEncoding = true) { libxml_use_internal_errors(true); libxml_clear_errors(); if ($setEncoding && $this->doc instanceof DOMDocument && $this->doc->encoding) { $text = "<?xml version='1.0' encoding='{$this->doc->encoding}'?>" . $text; } $doc_uri = $doc->documentURI; if (!$doc->loadXML($text, $this->loadOptions)) { $this->raiseError($this->xmlError(libxml_get_errors(), "Could not load XML")); libxml_clear_errors(); libxml_use_internal_errors(false); return false; } $doc->documentURI = $doc_uri; libxml_clear_errors(); $includes = $doc->xinclude(); $errors = libxml_get_errors(); while (count($errors) > 0 || $includes > 0) { /* doing this in a while loop because of: http://us.php.net/manual/en/function.domdocument-xinclude.php#44954 */ if (count($errors) > 0) { $this->raiseError($this->xmlError($errors, "Could not load XML's xinclude with document URI:\n" . $doc->documentURI)); libxml_clear_errors(); libxml_use_internal_errors(false); return false; } libxml_clear_errors(); $includes = $doc->xinclude(); $errors = libxml_get_errors(); } libxml_use_internal_errors(false); return true; }
public function testXInclude_xinclude() { $xml = new DOMDocument(); $this->assertFalse($xml->substituteEntities); $this->assertFalse($xml->resolveExternals); $xml->load("../../xml_files_windows/xinclude.xml"); $xml->xinclude(); $content_el = $xml->getElementsByTagName("content"); $content = $content_el->item(0)->nodeValue; $content = preg_replace('/\\s+/', '', $content); $this->assertEquals("it_works", $content); }
/** * Loads the configuration from the passed filename. * * @param string $filename The filename to load the configuration from * * @return \DOMDocument The parsed Configuration * @throws \Exception */ public function loadConfigurationByFilename($filename) { // initialize the DOMDocument with the configuration file to be validated $configurationFile = new \DOMDocument(); $configurationFile->load($filename); // substitute xincludes $configurationFile->xinclude(LIBXML_SCHEMA_CREATE); // create a DOMElement with the base.dir configuration $paramElement = $configurationFile->createElement('param', APPSERVER_BP); $paramElement->setAttribute('name', DirectoryKeys::BASE); $paramElement->setAttribute('type', ParamNode::TYPE_STRING); // create an XPath instance $xpath = new \DOMXpath($configurationFile); $xpath->registerNamespace('a', 'http://www.appserver.io/appserver'); // for node data in a selected id $baseDirParam = $xpath->query(sprintf('/a:appserver/a:params/a:param[@name="%s"]', DirectoryKeys::BASE)); if ($baseDirParam->length === 0) { // load the <params> node $paramNodes = $xpath->query('/a:appserver/a:params'); // load the first item => the node itself if ($paramsNode = $paramNodes->item(0)) { // append the base.dir DOMElement $paramsNode->appendChild($paramElement); } else { // throw an exception, because we can't find a mandatory node throw new \Exception('Can\'t find /appserver/params node'); } } // create a new DOMDocument with the merge content => necessary because else, schema validation fails!! $doc = new \DOMDocument(); $doc->loadXML($configurationFile->saveXML()); // return the XML document return $doc; }
/** * Substitutes XIncludes present in the current document. * * @param int $options libXml flags to use for loading (like LIBXML_NOCDATA etc.) * * @return int number of XIncludes or false if no xincludes were found * * @throws \DOMException in case of errors xincluding content */ public function xinclude($options = 0) { $user_error_handling = $this->enableErrorHandling(); $number_of_xincludes = parent::xinclude($options); $this->handleErrors('Resolving XInclude directives in the current document failed. Details are:' . PHP_EOL . PHP_EOL, PHP_EOL . 'Please fix the XInclude directives according to the mentioned errors.', $user_error_handling); return $number_of_xincludes; }
function saveTransformOutput($recHMLFilename, $styleFilename, $outputFilename = null) { global $outputURI; $recHmlDoc = new DOMDocument(); if (preg_match("/http/", $recHMLFilename)) { $suc = $recHmlDoc->loadXML(loadRemoteFile($recHMLFilename)); } else { $suc = $recHmlDoc->load($recHMLFilename); } if (!$suc) { returnXMLErrorMsgPage("Unable to load file {$recHMLFilename}"); } $recHmlDoc->xinclude(); //todo write code here to squash xincludes down to some limit. if (!$styleFilename) { if (!$outputFilename) { returnXMLErrorMsgPage("No transform filename or outputFilename provided for {$recHMLFilename}"); } if (is_logged_in()) { $cntByte = $recHmlDoc->saveHTMLFile($outputFilename); } if ($cntByte > 0) { returnXMLSuccessMsgPage("Successfully wrote {$cntByte} bytes of untransformed file {$recHMLFilename} to {$outputFilename}"); } else { returnXMLErrorMsgPage("Unable to output untransformed file {$recHMLFilename} to {$outputFilename}"); } } else { $xslDoc = new DOMDocument(); if (preg_match("/http/", $styleFilename)) { $suc = $xslDoc->loadXML(loadRemoteFile($styleFilename)); } else { $suc = $xslDoc->load($styleFilename); } if (!$suc) { returnXMLErrorMsgPage("Unable to load XSLT transform file {$styleFilename}"); } } $xslProc = new XSLTProcessor(); $xslProc->importStylesheet($xslDoc); // set up common parameters for stylesheets. $xslProc->setParameter('', 'hbaseURL', HEURIST_BASE_URL); $xslProc->setParameter('', 'dbName', HEURIST_DBNAME); $xslProc->setParameter('', 'dbID', HEURIST_DBID); $xslProc->setParameter('', 'transform', $styleFilename); $xslProc->setParameter('', 'standalone', '1'); if ($outputFilename && is_logged_in()) { $cntByte = $xslProc->transformToURI($recHmlDoc, $outputFilename); if ($cntByte > 0) { returnXMLSuccessMsgPage("Successfully wrote {$cntByte} bytes of {$recHMLFilename} transformed by {$styleFilename} to {$outputFilename}" . ($outputURI ? " <a href=\"{$outputURI}\" target=\"_blank\">{$outputURI}</a>" : "Unable to determine URI for {$outputFilename} because is does not match website path!")); } else { returnXMLErrorMsgPage("Unable to transform and/or output file {$recHMLFilename} transformed by {$styleFilename} to {$outputFilename}"); } } else { $doc = $xslProc->transformToDoc($recHmlDoc); // echo $xslProc->transformToXML($recHmlDoc); echo $doc->saveHTML(); } }