如何使用 PHP 将 XPath 表达式作为 XSL 参数传递? [英] How do I pass an XPath expression as an XSL param using PHP?

查看:26
本文介绍了如何使用 PHP 将 XPath 表达式作为 XSL 参数传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的 XML 文件,我正在从中提取信息并将其转换为不同的 XML 格式.我的转换本身运行得很完美,所以现在我想通过使用 PHP 传入一个变量 select="" 条件来使其更加灵活.

I have a very large XML file that I'm extracting information from, and transforming it into a different XML format. I have the transformation itself running perfectly, so now I would like to make it more flexible by using PHP to pass in a variable select="" criteria.

以下是我正在使用的工作 XSLT 代码(为简单起见被截断):

The following is the working XSLT code I'm using (truncated for simplicity):

<xsl:param name="criteria" select="//Product[PublicationDate &gt; 20141231]" />
<xsl:template match="ONIXMessage">
 <xsl:for-each select="$criteria">
  <xsl:apply-templates select="Title"/>
 </xsl:for-each>
</xsl:template>

$criteria 的值是一个 XPath 表达式,上面的工作没有问题.问题是当我尝试使用 PHP 将不同的值传递给 $criteria 时:

The value of $criteria is an XPath expression, and the above works without trouble. The issue is when I try to use PHP to pass a different value into $criteria:

$proc = new XSLTProcessor;
$proc->importStyleSheet( $icml );
$proc->setParameter( "", "criteria", "//Product[Imprint = 'Nightwood']");

当我使用 setParameter 时,转换完全失败.我知道表达式本身是准确的,因为当我将它们直接输入到 XSLT(没有 PHP)时,transfrom 工作正常.

The transformation fails entirely when I use setParameter. I know the expressions themselves are accurate because the transfrom works fine when I enter them directly into the XSLT (without PHP).

据我所知,问题是参数作为字符串而不是正确的 XPath 表达式传递,因此 XSLT 无法将 select="" 解析为节点集.那么我该如何预防呢?传递整个 XPath 表达式很重要,这样我就可以将 for-each 条件更改为任何我想要的.

From what I understand, the problem is that the parameter is being passed as a string rather than as a proper XPath expression, and so the XSLT cannot resolve select="" to a node-set. So how do I prevent that? It's important to pass a whole XPath expression so that I can change the for-each criteria to whatever I want.

推荐答案

据我所知,问题是参数被作为字符串而不是正确的 XPath 表达式传递

From what I understand, the problem is that the parameter is being passed as a string rather than as a proper XPath expression

这是一个正确的评估.您需要一个可以将字符串计算为 XPath 表达式的处理器.幸运的是,PHP 使用的 libxslt 处理器确实支持 EXSLT dyn:evaluate() 扩展函数.

That is a correct assessment. You need a processor that can evaluate the string as an XPath expression. Fortunately for you, the libxslt processor used by PHP does support the EXSLT dyn:evaluate() extension function.

如果没有这个,您将不得不将参数传递给另一个 XSLT 样式表,其功能是生成实际转换所需的 XSLT 样式表.

Without this, you would have to pass the parameter to another XSLT stylesheet, whose function would be to generate the XSLT styleshhet required for the actual transform.

这篇关于如何使用 PHP 将 XPath 表达式作为 XSL 参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆