1

我正在尝试进行 XSLT 转换(服务器端,使用 php),并且我希望 XHTML 作为该转换的输出,因为它是 XHTML 页面的一部分。如果我使用

    <xsl:output method="html" /> 

它生成的代码是有效的 html 代码,但具有 br 标记(不带 /),因此它不会验证为 xhtml;

如果我使用

    <xsl:output method="xml" /> 

荒谬来了:输出确实有 br/ 标签,并且是正确的 xml,但是当我将它放入它所属的页面时,一些结束标签会无缘无故地消失。

这是 XSL 文件http://hbxplayer.sf.net/test/mobile.xsl(这里粘贴有点长)

进行转换的 PHP 是这样的:

    ob_start();
    include 'presetList_xml.php';
    $xml=ob_get_clean();
    $xsl=file_get_contents("mobile.xsl");
    $xslt=new XSLTProcessor();
    $xslt->importStyleSheet(new SimpleXMLElement($xsl));
    echo $xslt->transformToXml(new SimpleXMLElement($xml));

加载输出的 JS 是这样的:

    function loadPresetList(){
        var listArea=document.getElementById("presetsArea");
        var xmlhttp=new XMLHttpRequest();
        xmlhttp.onreadystatechange=function(){
            if(xmlhttp.readyState==4){
                if(xmlhttp.status==200||xmlhttp.status==0){
                        listArea.innerHTML=xmlhttp.responseText;
                }else{ //ERROR, TRY AGAIN
                        listArea.innerHTML="Error. Retrying...";
                        loadPresetList();
                }
            }
        }
        xmlhttp.open("GET","presets_transformer.php?type=mobile&r="+Math.random(),true);
        xmlhttp.send();
    }

如您所见,没有什么特别的;但这是我第一次使用 XSLT,所以我可能做错了什么。

提前致谢 :)

编辑:在每个元素周围放置一个 div 似乎可以解决这个问题。为什么?我不知道。仍然:打开的第一个标签总是缺少结束标签

4

1 回答 1

1

Well if you want to use an XSLT 1.0 processor then it usually only supports either output method html to generate HTML 4 to be served as text/html or output method xml to generate XML or XHTML to be served with an XML MIME type like application/xml or to be served with the XHTML MIME type application/xhtml+xml. However older IE version (including IE 8) don't support XHTML served as application/xhtml+xml. And innerHTML is known to have problems with XHTML as well, in particular in older browsers.

XSLT 2.0 has an output method xhtml to generate well-formed XHTML that can be served as text/html and that would be the right option in my view of you want to deal with XHTML to be sent to older browsers.

Given that you use PHP which only supports XSLT 1.0 and the use of innerHTML I strongly suggest to have the XSLT stylesheet create HTML 4 or HTML 5 and to avoid XHTML.

于 2013-08-13T10:36:50.213 回答