在PHP中设置XSLTProcessor流上下文

时间:2014-05-14 21:34:38

标签: php xml xslt cookies

在XSL样式表中处理XSLTProcessor()命令时,如何告诉PHP document()对象发送一些HTTP头(如cookie,user-agent,...)? /强>


用例

这是HTTP请求所要求的几乎无效的XML(index.xml

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="process.xsl"?>

<data>
    <coucou/>
</data>

使用该XSL(process.xsl

进行处理
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="utf-8" indent="yes"/>

    <xsl:variable name="doc" select="document('cookie-en-xml.php')"/>
    <xsl:template match="/">
        <p>
            <xsl:value-of select="$doc/data/cookie"/>
        </p>
    </xsl:template>
</xsl:stylesheet>

调用此PHP文档,通过document('cookie-en-xml.php')输出XML:

<?xml version="1.0" encoding="utf-8"?>
<data>
    <cookie>
    <?php
        echo( htmlspecialchars($_COOKIE['peanuts'], ENT_QUOTES|ENT_XML1, 'UTF-8', true)  );
    ?>
    </cookie>
</data>

在客户端处理时没问题:用户代理调用第一个几乎无效的XML(index.xml),然后执行调用process.xsl发送cookie和其他cookie-en-xml.php的{​​{1}} HTTP标头。

在服务器端构建时,客户端调用xsl-builder.php

    $streamContext = stream_context_create($streamContextOptions);
// set stream context with cookies, UA, and so on
// this stream is based on client's cookies and UA,
// so I would like these headers to be send
// when the "document()" calls are processed in the XSL

libxml_set_streams_context($streamContext);
// I though it would also apply for the ``document()`` inside the XSL...
// but apparently, it does not...

$xmlDocument = new DOMDocument();
$xmlDocument->load('cookie-en-xml.php');
// because here, the dynamic XML is finely loaded:
// cookies values appear if I var_dump the $xmlDocument::C14N()
// ...

$xslDocument = new DOMDocument();
$xslDocument->load($xslURI);

$xmlDocument = new DOMDocument();
$xmlDocument->load($xmlURI);

$xslProcessor = new XSLTProcessor();
$xslProcessor->importStylesheet($xslDocument);
// ...but here, the document() calls are made by the XSLTProcessor without cookies

$htmlDocument = $xslProcessor->transformToDoc($xmlDocument);
// The http headers received by ``cookie-en-xml.php`` is void,
// so is the output after execution of the XSL

问题XSLTProcessor()调用cookie-en-xml.php而不发送cookie或用户代理(因为它是服务器,而不是处理XSL的客户端)。

如何在处理XSLTProcessor()命令时告诉document()发送一些HTTP标头(cookie,用户代理......)?

由于

0 个答案:

没有答案