2016-02-24 45 views
1

我正在使用XSL转换XML文档。有没有办法在声明<!doctype html>之前用PHP代码构建模板?xsl中的doctype之前的PHP代码

<!-- I want some PHP code to go here --> 
<xsl:processing-instruction name="php"> 
error_reporting(0); 
define('__ROOT__', dirname(dirname($_SERVER["DOCUMENT_ROOT"]))); 
require_once(__ROOT__."/classes/CasController.class.php"); 
$cas = new CasController(); 
?</xsl:processing-instruction> 
<!-- XSL content goes after here --> 
<xsl:output method="html" version="5.0" indent="yes" encoding="UTF-8" include-content-type="no"/> 

<xsl:strip-space elements="*"/> 

<xsl:template match="/document"> 
    <!-- begin html --> 
    <html lang="en"> 
     <head> 
     </head> 
     <body> 
     </body> 
    </html> 
    <!-- end html --> 
</xsl:template> 

输出中的HTML将

<?php error_reporting(0); 
define('__ROOT__', dirname(dirname($_SERVER["DOCUMENT_ROOT"]))); 
require_once(__ROOT__."/classes/Controller.class.php");?> 

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    </head> 
    <body> 
    </body> 
</html> 
+0

我得到的错误是:

<xsl:processing-instruction name="php"> define('__ROOT__', dirname(dirname($_SERVER["DOCUMENT_ROOT"]))); ?</xsl:processing-instruction> 

然后通过使用XML输出转义创建的文档类型标签:元素xsl:处理指令不能直接出现在xsl:stylesheet中 –

回答

0

要包含PHP的<!DOCTYPE HTML>之前,使用<xsl:output method="html" indent="yes" encoding="UTF-8" include-content-type="no" />没有version="5.0"属性。这将声明输出为HTML,但不包含<!DOCTYPE HTML>

插入,你希望它在你的模板去你的PHP处理指令:

<xsl:text disable-output-escaping="yes">&lt;!DOCTYPE HTML&gt;</xsl:text>