且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将URL作为参数传递给XSL

更新时间:2022-06-27 02:40:26

也许您已经尝试过这种方法,但是如果没有,请尝试以下方法:

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