/** * Adds a style element with $css to $document. * * This method is protected to allow overriding. * * @see https://github.com/jjriv/emogrifier/issues/103 * * @param \DOMDocument $document * @param string $css * * @return void */ protected function addStyleElementToDocument(\DOMDocument $document, $css) { $styleElement = $document->createElement('style', $css); $styleAttribute = $document->createAttribute('type'); $styleAttribute->value = 'text/css'; $styleElement->appendChild($styleAttribute); $body = $document->getElementsByTagName('body')->item(0); if (!$body) { $body = $document->createElement('body'); $document->insertBefore($body); } if ($this->appendStylesToBody) { $body->insertBefore($styleElement, $body->firstChild); } else { $head = $this->getOrCreateHeadElement($document); $head->appendChild($styleElement); } }
function viewCCD($ccr, $raw = "no", $requested_by = "") { global $pid; $ccr->preserveWhiteSpace = false; $ccr->formatOutput = true; $ccr->save(dirname(__FILE__) . '/generatedXml/ccrForCCD.xml'); $xmlDom = new DOMDocument(); $xmlDom->loadXML($ccr->saveXML()); $ccr_ccd = new DOMDocument(); $ccr_ccd->load(dirname(__FILE__) . '/ccd/ccr_ccd.xsl'); $xslt = new XSLTProcessor(); $xslt->importStylesheet($ccr_ccd); $ccd = new DOMDocument(); $ccd->preserveWhiteSpace = false; $ccd->formatOutput = true; $ccd->loadXML($xslt->transformToXML($xmlDom)); $ccd->save(dirname(__FILE__) . '/generatedXml/ccdDebug.xml'); if ($raw == "yes") { // simply send the xml to a textarea (nice debugging tool) echo "<textarea rows='35' cols='500' style='width:95%' readonly>"; echo $ccd->saveXml(); echo "</textarea>"; return; } if ($raw == "pure") { // send a zip file that contains a separate xml data file and xsl stylesheet if (!class_exists('ZipArchive')) { displayError(xl("ERROR: Missing ZipArchive PHP Module")); return; } $tempDir = $GLOBALS['temporary_files_dir']; $zipName = $tempDir . "/" . getReportFilename() . "-ccd.zip"; if (file_exists($zipName)) { unlink($zipName); } $zip = new ZipArchive(); if (!$zip) { displayError(xl("ERROR: Unable to Create Zip Archive.")); return; } if ($zip->open($zipName, ZIPARCHIVE::CREATE)) { $zip->addFile("stylesheet/cda.xsl", "stylesheet/cda.xsl"); $xmlName = $tempDir . "/" . getReportFilename() . "-ccd.xml"; if (file_exists($xmlName)) { unlink($xmlName); } $e_styleSheet = $ccd->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="stylesheet/cda.xsl"'); $ccd->insertBefore($e_styleSheet, $ccd->firstChild); $ccd->save($xmlName); $zip->addFile($xmlName, basename($xmlName)); $zip->close(); header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Length: " . filesize($zipName)); header("Content-Disposition: attachment; filename=" . basename($zipName) . ";"); header("Content-Description: File Transfer"); readfile($zipName); unlink($zipName); unlink($xmlName); exit(0); } else { displayError(xl("ERROR: Unable to Create Zip Archive.")); return; } } if (substr($raw, 0, 4) == "send") { $recipient = trim(stripslashes(substr($raw, 5))); $result = transmitCCD($ccd, $recipient, $requested_by); echo htmlspecialchars($result, ENT_NOQUOTES); return; } $ss = new DOMDocument(); $ss->load(dirname(__FILE__) . "/stylesheet/cda.xsl"); $xslt->importStyleSheet($ss); $html = $xslt->transformToXML($ccd); echo $html; }
/** * Add a <?xyl-meta?> processing-instruction (only that). * * @param array $attributes Attributes. * @return void */ public function addMeta(array $attributes) { $handle = null; foreach ($attributes as $key => $value) { $handle .= $key . '="' . str_replace('"', '\\"', $value) . '" '; } $this->_mowgli->insertBefore(new \DOMProcessingInstruction('xyl-meta', substr($handle, 0, -1)), $this->_mowgli->documentElement); return; }
/** * Writes the RDF file on the file system. * * @access public * @author Jerome Bogaerts, <*****@*****.**> * @return mixed */ public function write() { $targetPath = $this->getFilePath(); $file = $this->getTranslationFile(); $semanticNamespaces = array('rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#'); $xmlNS = 'http://www.w3.org/XML/1998/namespace'; $xmlnsNS = 'http://www.w3.org/2000/xmlns/'; try { $targetFile = new DOMDocument('1.0', 'UTF-8'); $targetFile->formatOutput = true; // Create the RDF root node and annotate if possible. $rdfNode = $targetFile->createElementNS($semanticNamespaces['rdf'], 'rdf:RDF'); $targetFile->appendChild($rdfNode); $rootAnnotations = $this->getTranslationFile()->getAnnotations(); if (count($rootAnnotations)) { $annotationsString = tao_helpers_translation_RDFUtils::serializeAnnotations($rootAnnotations); $annotationsNode = $targetFile->createComment("\n " . $annotationsString . "\n"); $targetFile->insertBefore($annotationsNode, $rdfNode); } $rdfNode->setAttributeNS($xmlNS, 'xml:base', $file->getBase()); $rdfNode->setAttributeNS($xmlnsNS, 'xmlns:rdfs', $semanticNamespaces['rdfs']); $xPath = new DOMXPath($targetFile); $xPath->registerNamespace($semanticNamespaces['rdf'], 'rdf'); $xPath->registerNamespace($semanticNamespaces['rdfs'], 'rdfs'); foreach ($file->getTranslationUnits() as $tu) { // Look if the predicate is a semantic namespace. $uri = explode('#', $tu->getPredicate()); if (count($uri) == 2) { $namespace = $uri[0] . '#'; $qName = $uri[1]; if (($searchedNS = array_search($namespace, $semanticNamespaces)) !== false) { $tuNode = $targetFile->createElement("{$searchedNS}:{$qName}"); $tuNode->setAttributeNS($xmlNS, 'xml:lang', $tu->getTargetLanguage()); $cdataValue = $tu->getTarget() == '' ? $tu->getSource() : $tu->getTarget(); $tuNodeValue = $targetFile->createCDATASection($cdataValue); $tuNode->appendChild($tuNodeValue); // Check if an rdf:Description exists for // the target of the TranslationUnit. $subject = $tu->getSubject(); $result = $xPath->query("//rdf:Description[@rdf:about='{$subject}']"); if ($result->length > 0) { // Append to the existing rdf:Description. $existingDescription = $result->item(0); $existingDescription->appendChild($tuNode); } else { // Append to a new rdf:Description node. $descriptionNode = $targetFile->createElementNS($semanticNamespaces['rdf'], 'rdf:Description'); $descriptionNode->setAttributeNS($semanticNamespaces['rdf'], 'rdf:about', $subject); $descriptionNode->appendChild($tuNode); $rdfNode->appendChild($descriptionNode); } // Finally add annotations. $annotations = $tu->getAnnotations(); if (count($annotations) > 0) { $annotationString = "\n " . tao_helpers_translation_RDFUtils::serializeAnnotations($annotations) . "\n "; $annotationNode = $targetFile->createComment($annotationString); $tuNode->parentNode->insertBefore($annotationNode, $tuNode); } } } } $targetFile->save($targetPath); } catch (DOMException $e) { throw new tao_helpers_translation_TranslationException("An error occured while writing the RDF File at '{$targetPath}'."); } }
public function render() { $dom = $dom = new \DOMDocument('1.0'); $header = $dom->createElement('h1', $this->_name); $dom->appendChild($header); $form = $dom->createElement('form'); $form->setAttribute('action', $this->_action); $form->setAttribute('method', $this->_method); $form->setAttribute('id', $this->_id); if ($this->_enctype != null) { $form->setAttribute('enctype', $this->_enctype); } $dom->appendChild($form); foreach ($this->_fields as $name => $field) { if (($field->type == self::FIELD_INPUT_HIDDEN || $field->type == self::FIELD_NOT_RENDER) && $field->error != null) { $err = $dom->createElement('div', $field->error); $err->setAttribute('class', 'form-field-error'); $dom->insertBefore($err, $form); } if (($rendered = $this->createField($name)->render($dom)) != null) { $form->appendChild($rendered); } } return $dom->saveHTML(); }
<?php $doc = new DOMDocument(); $node1 = $doc->createElement('div'); $node2 = $doc->createElement('div'); $res1 = $doc->appendChild($node1); $res2 = $doc->insertBefore($node2, $res1); $res3 = $doc->insertBefore($node1, $node2); var_dump($doc->saveXML());