01/10/2018, 17:18
Cách debug trong XSLT trong PHP
Khi bạn sử dụng phương thức transformToXML của lớp XSLTProcessor, nếu bạn nhận được giá trị là “false” thì bạn sử dụng 2 hàm là libxml_get_last_error() hoặc libxml_get_errors() để tìm lỗi. Ví dụ như đoạn mã sau function parseXML($fileXML, $fileXSL){ $XML = new DOMDocument(); ...
Khi bạn sử dụng phương thức transformToXML của lớp XSLTProcessor, nếu bạn nhận được giá trị là “false” thì bạn sử dụng 2 hàm là libxml_get_last_error() hoặc libxml_get_errors() để tìm lỗi. Ví dụ như đoạn mã sau
function parseXML($fileXML, $fileXSL){
$XML = new DOMDocument();
$XML->loadXML($fileXML);
$xslt = new XSLTProcessor();
$XSL = new DOMDocument();
$XSL->load($fileXSL);
$xslt->registerPHPFunctions();
$xslt->importStylesheet($XSL);
return $xslt->transformToXML($XML);
}
Khi hàm parseXML() được gọi và giá trị trả về là ‘false’ thì bạn có thể tiến hành debug như sau:
function parseXML($fileXML, $fileXSL){
$XML = new DOMDocument();
$XML->loadXML($fileXML);
$xslt = new XSLTProcessor();
$XSL = new DOMDocument();
$XSL->load($fileXSL);
$xslt->registerPHPFunctions();
$xslt->importStylesheet($XSL);
$xslt->transformToXML($XML);
echo libxml_get_last_error();
}
Happy coding 