【问题标题】:How to find problem with PHP XSLTProcessor when return from transformToXML is false and libxml_get_errors returns nothing当从 transformToXML 返回为 false 并且 libxml_get_errors 不返回任何内容时,如何找到 PHP XSLTProcessor 的问题
【发布时间】:2011-02-28 06:26:43
【问题描述】:

我正在编写以下代码,以允许无法执行 XSL 转换的 HTTP 用户代理查看我服务器上的资源。我很困惑,因为 transformToXML 的结果是false,但libxml_get_errors() 的结果是一个空数组。如您所见,代码输出 LibXSLT 版本 ID,我在 WinVista 版本 1.1.24 上遇到了问题。 libxml_get_errors() 不是从 XSLTProcessor 对象获取错误的正确函数吗?

如果您对 XML 文档感兴趣,可以从 http://bobberinteractive.com/index.xhtml 和 .../stylesheets/layout.xsl 获取它们

<?php //redirect browsers that can handle the source files. if (strpos ( $_SERVER ['HTTP_ACCEPT'], 'application/xhtml+xml' )) { header ( "HTTP/1.1 301 Moved Permanently" ); header ( "Location: http://" . $_SERVER ['SERVER_NAME'] . "/index.xhtml" ); header ( "Content-Type: text/text" ); echo "\nYour browser is capable of processing the <a href='/index.xhtml'> site contents on its own."; die (); } //start by checking the template $baseDir = dirname ( __FILE__ ); $xslDoc = new DOMDocument (); if (! $xslDoc->load ( $baseDir . '/stylesheets/layout.xsl' )) { header ( "HTTP/1.1 500 Server Error" ); header ( "Content-Type: text/plain" ); echo "\n Can't load " . $baseDir . '/stylesheets/layout.xsl'; die (); } //resolve the requested resource (browsers that need transformation will request the resource without the suffix) $uri = $_SERVER ['REQUEST_URI']; $len = strlen ( $uri ); if (1 >= $len || '/' == substr ( $uri, $len - 1 )) { $fileName = $baseDir . "/index.xhtml"; // use 'default' document if pathname ends in '/' } else { $fileName = $baseDir . (1 load ( $fileName )) { header ( "HTTP/1.1 500 Server Error" ); echo "\n Can't load " . $fileName; die (); } // now start the XSL template processing $proc = new XSLTProcessor (); $proc->importStylesheet ( $xslDoc ); $doc = $proc->transformToXML ( $xmlDoc ); if (false === $doc) { header ( "HTTP/1.1 500 Server Error" ); header ( "Content-Type: text/plain" ); echo "\n"; // HERE is where it gets strange: the value of $doc is false and libxml_get_errors returns 0 entries. display_xml_errors ( libxml_get_errors() ); die (); } header ( "Content-Type: text/html" ); echo "\n"; echo $doc; function display_xml_errors($errors) { echo count ( $errors ) . " Error(s) from LibXSLT " . LIBXSLT_DOTTED_VERSION; for($i = 0; $i level) { case LIBXML_ERR_WARNING : $return .= "Warning $error->code: "; break; case LIBXML_ERR_ERROR : $return .= "Error $error->code: "; break; case LIBXML_ERR_FATAL : $return .= "Fatal Error $error->code: "; break; } $return .= trim ( $error->message ) . "\n Line: $error->line" . "\n Column: $error->column"; if ($error->file) { $return .= "\n File: $error->file"; } echo "$return\n\n--------------------------------------------\n\n"; } }

【问题讨论】:

  • 感谢您提供文件。附带说明:Opera 可以处理 index.xhtml,但是您的网络服务器发送 Content-Type: */* 而不是 application/xhtml+xml,因此它不会被处理。易于修复,非常感谢小型 Opera 社区。​​span>
  • @Wrikken:感谢您注意到 mime 类型问题。我正在使用类型映射来查看使用 http-equiv head/meta 元素指定 Content-Type 可以获得什么效果。

标签: php xml xslt


【解决方案1】:

我在加载 XML 或执行 XSL 时遇到了几个错误。升级你的error_reporting 级别

error_reporting(E_ALL | E_STRICT);
// or
error_reporting(-1); // overzealous, but works

找到我了:

PHP Warning:  DOMDocument::load(): Entity 'ndash' not defined in /tmp/index.xhtml, line: 8 in /tmp/test.php on line 4
PHP Warning:  XSLTProcessor::importStylesheet(): compilation error: file /tmp/layout.xsl line 59 element a in /tmp/test.php on line 10
PHP Warning:  XSLTProcessor::importStylesheet(): Attribute 'onclick': The content is expected to be a single text node when compiling an AVT. in /tmp/test.php on line 10
PHP Warning:  XSLTProcessor::importStylesheet(): compilation error: file /tmp/layout.xsl line 185 element a in /tmp/test.php on line 10
PHP Warning:  XSLTProcessor::importStylesheet(): Attribute 'onclick': The content is expected to be a single text node when compiling an AVT. in /tmp/test.php on line 10
PHP Warning:  XSLTProcessor::transformToXml(): No stylesheet associated to this object in /tmp/test.php on line 12

【讨论】:

  • 不要过分热心,在开发过程中非常需要报告,而不必担心新发明的错误类型。
  • 我也是:有时(嗯,很少)会添加错误常量,因此 max int -1 将始终捕获它们。 E_ALL 不像它应该的那样工作,恕我直言。 E_REALLY_ALL 的时间
猜你喜欢
  • 1970-01-01
  • 2019-03-27
  • 1970-01-01
  • 1970-01-01
  • 2018-03-02
  • 2020-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多