将URL作为参数传递给XSL [英] Pass URL as a parameter to XSL
问题描述
我想将当前页面的URL作为属性传递给XSL模板.据我了解,它应该作为参数传递,然后用作属性.
I'd like to pass current page URL as an attribute to XSL template. As far as I understood it should be passed as a parameter, and then used as an attribute.
我使用PHP加载XML& XSL文件:
I use PHP to load XML & XSL files:
<?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"的参数传递?
How should this code be altered to pass URL as a parameter named "current-url", for example?
我在这里看到过很多类似的问题,但使用了不同的解决方案,但到目前为止,没有一个问题对我有用.预先谢谢你.
I've seen a lot of similar questions here with different solutions, but none has worked for me so far. Thank you in advance.
推荐答案
也许您已经尝试过这种方法,但是如果没有,请尝试以下方法:
Maybe you already tried this approach, but in case if not:
<?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中,在模板上方添加
In the xsl, add above the templates
<xsl:param name="current-url" />
在模板中,您可以使用
<xsl:value-of select="$current-url" />
如果还不存在,则必须在xsl:stylesheet声明中添加xmlns:php="http://php.net/xsl"
.
供参考: registerPHPFunctions()以及您可能已经在SO上检查过的解决方案: 将变量传递到XSLT
If not already there, you have to add xmlns:php="http://php.net/xsl"
into the xsl:stylesheet declaration.
For reference: registerPHPFunctions() and a solution you maybe already checked on SO: Passing variables to XSLT
这篇关于将URL作为参数传递给XSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!