带有 PHP XSL 转换器的 HTML 5 [英] HTML 5 with PHP XSL transformer

查看:27
本文介绍了带有 PHP XSL 转换器的 HTML 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PHP 中的 XSL 转换器生成有效的 HTML 5 输出,但我在这样做时遇到了困难.这是示例 PHP 代码:

</html></xsl:模板><xsl:template match="/"><xsl:apply-templates/></xsl:模板></xsl:stylesheet>排爆;$xml = 新的 DomDocument;$xml->LoadXML($xml_source);$xsl = 新的 DomDocument;$xsl->loadXML($xsl_source);$xslt = 新的 XSLTProcessor;$xslt->importStyleSheet( $xsl );echo $xslt->transformToXML( $xml );

<xsl:output method="html" 它生成

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><身体><div style="color: green"></div>这个文字应该是黑色的<br></br>这个黑色文本在下一行</html>

<br></br> 被解释为两个中断

<xsl:output method="xml" 它生成

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><身体><div style="color: green"/>这个文字应该是黑色的<br/>这个黑色文本在下一行</html>

<div/> 是自动关闭的,它被解释为只是一个打开的

并且文本是绿色的.

我需要你的建议,如何继续.PHP XSL 处理器中是否有一些未记录的选项可以使某些标签自关闭.是否有内置 XSLT 处理器的替代方案?

解决方案

我找到了一个解决方法.由于 XHTML 输出在 XSLT 1.0 中不可用,请执行以下操作:

首先,保持xsl:outputxml.

其次,通过在中间插入一些东西来强制某些标签使用单独的结束标签呈现.例如,在假设具有结束标记的空标记中添加 &#160;.像这样 <div style="color: green" ><xsl:comment/></div><script src="http://asdfasdf">&#160;</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">&lt;!DOCTYPE html&gt;&#10;</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 &#160; 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">&#160;</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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆