1

对于 PHP 5.3 中 transformToXML 的行为,我现在遇到了一个非常奇怪的情况。

我们可以说我的问题是请求客户端与服务器中完成的工作之间的关系是什么?

我没有任何与用户代理或任何类似内容和转换相关的规则。生成的 xml 不同,但始终有效。此外,差异涉及资源管理器的检测,但不涉及浏览器的版本。我总是将页面的最基本版本发送给资源管理器。

这是涉及的代码(php):

// var definition and initialization  
$temp['document'] = new DOMDocument();  
$xslDoc = new DOMDocument();  
$xslDoc->load( 'index.xslt' );  
$xslt = new XSLTProcessor();  
$xslt->importStylesheet( $xslDoc );  

...

// this dumps the xml document as it has been generated  
// $temp['document'] is generated above. Is valid and well formed xml  
var_dump( $temp['document'] );  

// load xml generated into DOMdocument object  
$xmlDoc->loadXML( $temp['document'] );  

// apply xsl transformation rules  
$final_doc = $xslt->transformToXML( $xmlDoc );  

// this dumps the tags writen in the xsl and the variables applied,  
// but not the transformations  
var_dump( $final_doc );  
exit( __FILE__.' '.__LINE__);  
...

如果我使用任何浏览器请求页面,包括正常模式下的 explorer 9,一切正常,没有错误并且页面生成。这包括 windows 和 linux 中的任何版本,以及在 browsershots 中使用更多版本进行测试。好吧,除了 konqueror。

如果我使用 explorer 6 或任何使用兼容性视图或 konqueror 4.8.5 的现代版本请求页面,则模板不起作用。

由于该过程是在服务器中发生的,因此我不理解这种行为。为什么 the<xsl:template match="some_tag">或 the<xsl:apply-templates />失败了?

我的日志或 libxml_get_last_error() 或 libxml_get_errors() 中没有错误。

任何有助于理解或解决此问题的帮助将不胜感激。

如果需要更多信息,我可以尝试发布它,请告诉我。

再见,谢谢

.xsl 文件

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:strip-space elements="*"/>
    <xsl:variable name="id_pagina">
        <xsl:choose>
            <xsl:when test="/pagina/id_pagina/@valor">
                <xsl:value-of select="/pagina/id_pagina/@valor" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:text>x</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:template match="/">
        <html>
            <body>
                <xsl:attribute name="id">
                    <xsl:value-of select="$id_pagina" />
                </xsl:attribute>
                <div id="Base">
                    <xsl:apply-templates />
                </div>

            </body>
        </html>
    </xsl:template>

    <xsl:template match="cabecera">
        <div id="zona_de_cabecera"><xsl:apply-templates /></div>
    </xsl:template>

    <xsl:template match="contenido">
        <div id="zona_de_contenido" class="shadow"><xsl:apply-templates select="node()"/></div>
    </xsl:template>

    <xsl:template match="pie[*]">
        <div id="zona_de_pie"><xsl:apply-templates select="node()"/></div>
    </xsl:template>

    <xsl:template match="div">
        <xsl:copy-of select="."/>
    </xsl:template>

    <xsl:template match="span">
        <xsl:copy-of select="."/>
    </xsl:template>

    <xsl:template match="p">
        <xsl:copy-of select="."/>
    </xsl:template>

    <xsl:template match="img">
        <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>
4

1 回答 1

0

那么在当前代码中

var_dump( $temp['document'] );

// load xsl
$xsl->importStylesheet( $xslDoc );
$xslDoc->loadXML( $temp['document'] );

// this dumps object(DOMDocument)[1]
var_dump( $xmlDoc );

// apply xsl
$final_doc = $xsl->transformToXML( $xslDoc );

// this dumps the tags writen in the xsl and the variables applied, but not the
// transformations
var_dump( $final_doc );

如发布的那样,您首先用于$xslDoc导入样式表,然后使用相同的变量 with$xslDoc->loadXML( $temp['document'] )来解析一些 XML 字符串,然后转储一个名为 的不同变量$xmlDoc,然后传递$xslDoc给 transformToXML。在我看来,好像您想使用两个不同的变量,其中$xslDoc应该是样式表和样式表$xmlDoc的输入,但是您的代码会以某种方式混淆这些变量,这样您就不会得到您想要的。因此,如果您的真实代码与发布的一样,那么我建议您确保使用两个不同的 DOM 文档,一个用于样式表,一个用于输入,然后将输入传递给 transformToXML。

于 2012-10-12T09:21:05.127 回答