【发布时间】: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 可以获得什么效果。