/** * Carrega o XSD. * @param string $fileName * @return \Sped\Generation\Schema * @throws \RuntimeException */ public function loadXmlSchema($fileName) { $fileName = realpath($fileName); if (!is_file($fileName)) { throw new \RuntimeException('This file ' . $fileName . ' not found'); } $this->loadedSchema = new \Sped\Components\Xml\Schema(); $this->loadedSchema->load($fileName, null, true); return $this; }
/** * Load imports and includes * * @param string $filepath Full path to first XSD Schema. * @param \DOMDocument $dom DOM model of the schema. * @throws \RuntimeException */ public function loadExternalFiles($filepath = null, $dom = null) { $dom = $dom instanceof \DOMDocument ? $dom : $this; $filepath = realpath(dirname(is_null($filepath) ? $this->fileName : $filepath)); $xpath = new \DOMXPath($dom); $readTags = array('import', 'include'); foreach ($readTags as $tagName) { $query = "//*[local-name()='{$tagName}' and namespace-uri()='http://www.w3.org/2001/XMLSchema']"; $entries = $xpath->query($query); if ($entries->length < 1) { continue; } foreach ($entries as $entry) { $xsdFileName = realpath($filepath . DIRECTORY_SEPARATOR . $entry->getAttribute("schemaLocation")); if (in_array($xsdFileName, $this->loadedImportFiles)) { $parent->removeChild($entry); continue; } if (!file_exists($xsdFileName)) { throw new \RuntimeException('File not found'); } $parent = $entry->parentNode; $xsd = new Schema(); $xsd->loadedImportFiles[] = $xsdFileName; if ($xsd->load($xsdFileName, null, true)) { $this->loadedImportFiles = array_merge($this->loadedImportFiles, $xsd->loadedImportFiles); } $targetNamespace = $xsd->getTargetNamespace(); $nsPrefix = $xsd->lookupPrefix($targetNamespace); foreach ($xsd->documentElement->childNodes as $node) { if (!$node instanceof \DOMElement) { continue; } if (empty($node->prefix)) { $node->prefix = $nsPrefix; } $newNode = $dom->importNode($node, true); $parent->insertBefore($newNode, $entry); } $parent->removeChild($entry); } } }