[英]HTML 5 with PHP XSL transformer
我正在尝试使用PHP中的XSL转换器生成有效的HTML 5输出,但这样做很难。 这是示例PHP代码:
<?php
$xml_source = '<?xml version="1.0" encoding="utf-8"?><content/>';
$xsl_source = <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" encoding="utf-8" />
<xsl:template match="content">
<xsl:text disable-output-escaping="yes"><!DOCTYPE html> </xsl:text>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<body>
<div style="color: green"></div>
This text should be black
<br/>
This black text is on next line
</body>
</html>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>
EOD;
$xml = new DomDocument;
$xml->LoadXML($xml_source);
$xsl = new DomDocument;
$xsl->loadXML($xsl_source);
$xslt = new XSLTProcessor;
$xslt->importStyleSheet( $xsl );
echo $xslt->transformToXML( $xml );
当<xsl:output method="html"
它会生成
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<body>
<div style="color: green"></div>
This text should be black
<br></br>
This black text is on next line
</body>
</html>
<br></br>
被解释为两个中断
当<xsl:output method="xml"
它会生成
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<body>
<div style="color: green" />
This text should be black
<br />
This black text is on next line
</body>
</html>
<div />
是自动关闭的,并且被解释为仅一个打开的<div>
并且文本为绿色。
我需要您关于如何进行的建议。 PHP XSL处理器中是否有一些未记录的选项可以使仅某些标签自动关闭。 内置XSLT处理器是否可以替代?
HTML没有自动关闭标签。 主要是因为HTML不是XML。
<br></br>
被解释为2个中断,因为<br>
标签没有结束标签,这就是为什么它尝试将其呈现为2个中断。
如果可以使用正确的DOCTYPE将其呈现为XHTML ,或者尝试其他方法。
我找到了解决方法。 由于XHTML输出在XSLT 1.0中不可用,请执行以下操作:
首先,将xsl:output
保留为xml
。
其次,通过在中间插入一些内容来强制某些标签使用单独的结束标签进行渲染。 例如,添加 
或<xsl:comment/>
在假定具有结束标记的空标记中。 像这样<div style="color: green" ><xsl:comment/></div>
或<script src="http://asdfasdf"> </script>
。 如果要在XSL中转换XHTML源代码,请执行类似的操作,但是当然要使用<!---->
而不是xsl:comment
第三,请确保不要使用XSL模板从标记中删除空的注释和空格。 在我的所有样式表中,我都包含类似的内容。
<xsl:template match="comment()|processing-instruction()">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
到目前为止,这对我来说非常有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.