Php 将URL作为参数传递给XSL
我想将当前页面URL作为属性传递给XSL模板。据我所知,它应该作为参数传递,然后作为属性使用 我使用PHP加载XML和XSL文件:Php 将URL作为参数传递给XSL,php,xml,url,xslt,parameters,Php,Xml,Url,Xslt,Parameters,我想将当前页面URL作为属性传递给XSL模板。据我所知,它应该作为参数传递,然后作为属性使用 我使用PHP加载XML和XSL文件: <?php $xml = new DOMDocument; $xml->load('main.xml'); $xsl = new DOMDocument; $xsl->load('blocks/common.xsl'); $proc = new XSLTProcessor; $proc->importStyleSheet($xsl);
<?php
$xml = new DOMDocument;
$xml->load('main.xml');
$xsl = new DOMDocument;
$xsl->load('blocks/common.xsl');
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
echo $proc->transformToXML($xml);
?>
例如,应该如何修改此代码以将URL作为名为“current URL”的参数传递
我在这里看到过很多类似的问题,都有不同的解决方案,但到目前为止,没有一个对我有效。提前谢谢。也许您已经尝试过这种方法,但如果没有:
<?php
$params = array('current-url' => $_SERVER['REQUEST_URI']);
$xml = new DOMDocument;
$xml->load('main.xml');
$xsl = new DOMDocument;
$xsl->load('blocks/common.xsl');
$proc = new XSLTProcessor;
$proc -> registerPHPFunctions();
$proc->importStyleSheet($xsl);
foreach ($params as $key => $val)
$proc->setParameter('', $key, $val);
echo $proc->transformToXML($xml);
?>
在xsl中,在模板上方添加
<xsl:param name="current-url" />
在模板中,可以使用
<xsl:value-of select="$current-url" />
如果还没有,则必须添加xmlns:php=”http://php.net/xsl“
导入xsl:stylesheet声明。供参考:您可能已经查看了一个解决方案,所以:谢谢matthias_h。现在尝试了这个方法,但是我在第28行得到了对非对象上的成员函数setParameter()的
调用,该非对象是$xsltProcessor->setParameter(“”,$key,$val)代码>无法理解,你有什么想法吗?刚刚更新了答案-出现了复制/粘贴错误。它应该是$proc->setParameter(…而不是$xsltProcessor->setParameter(。。