关于xml:当transformToXML返回为false并且libxml_get_errors不返回任何内容时,如何使用PHP XSLTProcessor查找问题

How to find problem with PHP XSLTProcessor when return from transformToXML is false and libxml_get_errors returns nothing

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

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

获取。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<wyn>
<?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"\
Your browser is capable of processing the  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"\
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"\
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"\
"
;
 // 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"\
"
;
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 ) ."\
 Line: $error->line"
."\
 Column: $error->column"
;

  if ($error->file) {
   $return .="\
 File: $error->file"
;
  }

  echo"$return\
\
--------------------------------------------\
\
"
;
 }
}
</wyn>


加载XML或执行XSL时出现几个错误。升级您的error_reporting级别

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

认识我:

1
2
3
4
5
6
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