gpt4 book ai didi

php - 如何将包含单引号和双引号的字符串作为参数传递给 PHP 中的 XSLT?

转载 作者:可可西里 更新时间:2023-10-31 23:44:20 26 4
gpt4 key购买 nike

我有一个简单的基于 PHP 的 XSLT 转换代码,看起来像这样:

$xsl = new XSLTProcessor();
$xsl->registerPHPFunctions();
$xsl->setParameter("","searchterms", $searchterms);
$xsl->importStylesheet($xslDoc);
echo $xsl->transformToXML($doc);

代码将包含字符串的变量 $searchterms 作为参数传递给 XSLT 样式表,XSLT 样式表又将其用作文本:

<title>search feed for <xsl:value-of select="$searchterms"/></title> 

这工作正常,直到你尝试传递一个包含 mixes 的字符串,比如:

$searchterms = '"some"'." text's quotes are mixed."

在这一点上,XSLT 处理器尖叫:

Cannot create XPath expression (string contains both quote and double-quotes)

安全地将任意字符串作为输入传递给 XSLT 的正确方法是什么?请注意,这些字符串将用作生成的 XML 中的文本值,而不是 XPATH 参数。

谢谢,波阿斯

最佳答案

这已被记录为错误:

https://bugs.php.net/bug.php?id=64137

这条评论:

This shortcoming comes from the fact that XPath 1.0 does not provide a mechanism to escape characters, so PHP does not have a straightforward way to express a string that contains both types of quotes. XPath 1.0 does, however, provide a function to concatenate strings. Using concat(), a string composed of the two characters "' can be expressed as concat('"',"'"). concat() takes 2 or more arguments.

包括以下解决方法:

So as long as you alternate the quoting style, you can express a string containing any number of quotes of both types.

另一种解决方案是将所有直引号替换为单引号:

$t = str_replace( "\"", "''", $t );
$xsltEngine->setParameter( "some-text", $t );

关于php - 如何将包含单引号和双引号的字符串作为参数传递给 PHP 中的 XSLT?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2736995/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com