您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

HTMLXSLTProcessor.php 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Decorator/extender XSLT processor specifically for HTML documents.
  4. */
  5. class ConfigDoc_HTMLXSLTProcessor
  6. {
  7. /**
  8. * Instance of XSLTProcessor
  9. */
  10. protected $xsltProcessor;
  11. public function __construct($proc = false)
  12. {
  13. if ($proc === false) $proc = new XSLTProcessor();
  14. $this->xsltProcessor = $proc;
  15. }
  16. /**
  17. * @note Allows a string $xsl filename to be passed
  18. */
  19. public function importStylesheet($xsl)
  20. {
  21. if (is_string($xsl)) {
  22. $xsl_file = $xsl;
  23. $xsl = new DOMDocument();
  24. $xsl->load($xsl_file);
  25. }
  26. return $this->xsltProcessor->importStylesheet($xsl);
  27. }
  28. /**
  29. * Transforms an XML file into compatible XHTML based on the stylesheet
  30. * @param $xml XML DOM tree, or string filename
  31. * @return string HTML output
  32. * @todo Rename to transformToXHTML, as transformToHTML is misleading
  33. */
  34. public function transformToHTML($xml)
  35. {
  36. if (is_string($xml)) {
  37. $dom = new DOMDocument();
  38. $dom->load($xml);
  39. } else {
  40. $dom = $xml;
  41. }
  42. $out = $this->xsltProcessor->transformToXML($dom);
  43. // fudges for HTML backwards compatibility
  44. // assumes that document is XHTML
  45. $out = str_replace('/>', ' />', $out); // <br /> not <br/>
  46. $out = str_replace(' xmlns=""', '', $out); // rm unnecessary xmlns
  47. if (class_exists('Tidy')) {
  48. // cleanup output
  49. $config = array(
  50. 'indent' => true,
  51. 'output-xhtml' => true,
  52. 'wrap' => 80
  53. );
  54. $tidy = new Tidy;
  55. $tidy->parseString($out, $config, 'utf8');
  56. $tidy->cleanRepair();
  57. $out = (string) $tidy;
  58. }
  59. return $out;
  60. }
  61. /**
  62. * Bulk sets parameters for the XSL stylesheet
  63. * @param array $options Associative array of options to set
  64. */
  65. public function setParameters($options)
  66. {
  67. foreach ($options as $name => $value) {
  68. $this->xsltProcessor->setParameter('', $name, $value);
  69. }
  70. }
  71. /**
  72. * Forward any other calls to the XSLT processor
  73. */
  74. public function __call($name, $arguments)
  75. {
  76. call_user_func_array(array($this->xsltProcessor, $name), $arguments);
  77. }
  78. }
  79. // vim: et sw=4 sts=4