并且文本是绿色的.
我需要你的建议,如何继续.PHP XSL 处理器中是否有一些未记录的选项可以使某些标签自关闭.是否有内置 XSLT 处理器的替代方案?
解决方案
我找到了一个解决方法.由于 XHTML 输出在 XSLT 1.0 中不可用,请执行以下操作:
首先,保持xsl:output
为xml
.
其次,通过在中间插入一些东西来强制某些标签使用单独的结束标签呈现.例如,在假设具有结束标记的空标记中添加  
或
.像这样 <div style="color: green" ><xsl:comment/></div>
或 <script src="http://asdfasdf"> </script>
.如果您在 XSL 中转换 XHTML 源代码,那么做类似的事情,但当然使用 <!---->
而不是 xsl:comment
第三,确保不要从带有 XSL 模板的标签中删除空注释和空格.在我所有的样式表中,我都包含这样的内容.
<xsl:copy><xsl:apply-templates/></xsl:copy></xsl:模板>
到目前为止,这对我来说效果很好.
I am trying to generate valid HTML 5 output using XSL transformer in PHP and I am having difficulty doing so. Here is the sample PHP code:
<?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 );
When <xsl:output method="html"
it generates
<!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>
gets interpreted into TWO breaks
When <xsl:output method="xml"
it generates
<!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>
The <div />
is self closing and that gets interpreted as just an opening <div>
and the text is green.
I need your advice on how to proceed. Is there some undocumented option in PHP XSL processor to make only some tags self-closing. Is there an alternative to built in XSLT processor?
解决方案
I found a workaround. Since XHTML output is not available in XSLT 1.0 do following:
First, keep xsl:output
as xml
.
Second, force certain tags to render with separate closing tag by inserting something in the middle. For example, add  
or <xsl:comment/>
in empty tags that suppose to have closing tag. like so <div style="color: green" ><xsl:comment/></div>
or <script src="http://asdfasdf"> </script>
. If you are converting XHTML source in XSL then do similar thing, but of course use <!---->
instead of xsl:comment
Third, make sure not to remove empty comments and spaces from tags with your XSL templates. In all my stylesheets I include something like this.
<xsl:template match="comment()|processing-instruction()">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
This has worked very well for me so far.
这篇关于带有 PHP XSL 转换器的 HTML 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!