/** * Cached transformation a given XML string using * the registered PHP callbacks for overloaded tags. * * @param string * @param string * @return string * @access public */ function transform($xml, $cacheID = '') { $cacheID = $cacheID != '' ? $cacheID : md5($xml); $cachedResult = $this->_cache->get($cacheID, 'XML_Transformer'); if ($cachedResult !== FALSE) { return $cachedResult; } $result = parent::transform($xml); $this->_cache->save($result, $cacheID, 'XML_Transformer'); return $result; }
require_once 'XML/Transformer.php'; function startTalk($attrs) { return "<presentation type='meeting_talk'>"; } function endTalk($cdata) { return "{$cdata}</presentation>"; } function startDate($attrs) { return "<year_month_day>"; } function endDate($cdata) { return "{$cdata}</year_month_day>"; } function startSpeaker($attrs) { return "<name role='speaker'>"; } function endSpeaker($cdata) { return "{$cdata}</name>"; } $options = array('overloadedElements' => array('talk' => array('start' => 'startTalk', 'end' => 'endTalk'), 'date' => array('start' => 'startDate', 'end' => 'endDate'), 'speaker' => array('start' => 'startSpeaker', 'end' => 'endSpeaker')), 'recursiveOperation' => false); $xmlfile = 'data/sdphp_talks.xml'; $xmldoc = implode('', file($xmlfile)); $t = new XML_Transformer($options); $newxml = $t->transform($xmldoc); echo $newxml;
/** * SAX callback for 'endElement' event. * * @param resource * @param string * @access private */ function _endElement($parser, $element) { $cdata = $this->_cdataStack[$this->_level]; $element = $this->canonicalize($element); $qElement = XML_Util::splitQualifiedName($element, '&MAIN'); $process = $this->_lastProcessed != $element; $recursion = FALSE; if ($process && isset($this->_callbackRegistry->overloadedNamespaces[$qElement['namespace']]['active'])) { // The event is handled by a callback // that is registered for this namespace. $result = $this->_callbackRegistry->overloadedNamespaces[$qElement['namespace']]['object']->endElement($qElement['localPart'], $cdata); if (is_array($result)) { $cdata =& $result[0]; $reparse = $result[1]; } else { $cdata =& $result; $reparse = TRUE; } $recursion = $reparse && isset($this->_elementStack[$this->_level - 1]) && $this->_callbackRegistry->overloadedNamespaces[$qElement['namespace']]['recursiveOperation']; } else { // No callback was registered for this element's // closing tag, copy it. $cdata .= '</' . $element . '>'; } if ($recursion) { // Recursively process this transformation's result. if ($this->_checkDebug('&RECURSE')) { $this->sendMessage(sprintf('start recursion[%d]: %s', $this->_level, $cdata)); } $transformer = new XML_Transformer(array('callbackRegistry' => &$this->_callbackRegistry, 'caseFolding' => $this->_caseFolding, 'caseFoldingTo' => $this->_caseFoldingTo, 'lastProcessed' => $element)); $cdata = substr($transformer->transform("<_>{$cdata}</_>"), 3, -4); if ($this->_checkDebug('&RECURSE')) { $this->sendMessage(sprintf('end recursion[%d]: %s', $this->_level, $cdata)); } } if ($this->_checkDebug($element)) { $this->sendMessage(sprintf('endElement[%d]: %s (with cdata=%s)', $this->_level, $element, $this->_cdataStack[$this->_level])); } // Move result of this transformation step to // the parent's CDATA section. $this->_cdataStack[--$this->_level] .= $cdata; }