Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将URL作为参数传递给XSL_Php_Xml_Url_Xslt_Parameters - Fatal编程技术网

Php 将URL作为参数传递给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);

我想将当前页面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);

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(。。