繁体   English   中英

在PHP中设置XSLTProcessor流上下文

[英]Set XSLTProcessor stream context in PHP

在XSL样式表中处理document()命令时,如何告诉PHP XSLTProcessor()对象发送一些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-en-xml.php发送cookie和其他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的客户端)。

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

谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM