2015-04-16 58 views
1

我有一個關於將參數從PHP代碼傳遞到xslt的問題。 這裏是我的PHP代碼:將字符串作爲參數傳遞到PHP中的XSLT中

$searchStr = 'article[@key = 'journals/acta/Saxena96']'; 
$xsltProcessor = new XSLTProcessor(); 
$xsltProcessor->registerPHPFunctions(); 
$xsltProcessor->importStyleSheet($this->xslDoc); 
$xsltProcessor->setParameter('', 'search_condition', $searchStr); 

這裏是XSLT:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="html"/> 
    <xsl:template match="dblp"> 
     <form> 
     <table border="1"> 
      <tr bgcolor="#9acd32"> 
       <th style="text-align:left">Auteur</th> 
       <th style="text-align:left">Titre</th> 
       <th style="text-align:left">Pages</th> 
       <th style="text-align:left">Volumne</th> 
       <th style="text-align:left">Journal</th> 
       <th style="text-align:left">Numéro</th> 
       <th style="text-align:left">URL</th> 
       <th style="text-align:left">EE</th> 
       <th style="text-align:left">Modification</th> 
      </tr> 
      <xsl:for-each select="$search_condition"> 
       <tr> 
        <td> 
         <xsl:value-of select="author/text()"/> 
        </td> 
        <td> 
         <xsl:value-of select="title/text()"/> 
        </td> 
        <td> 
         <xsl:value-of select="pages/text()"/> 
        </td> 
        <td> 
         <xsl:value-of select="volume/text()"/> 
        </td> 
        <td> 
         <xsl:value-of select="journal/text()"/> 
        </td> 
        <td> 
         <xsl:value-of select="number/text()"/> 
        </td> 
        <td> 
         <xsl:value-of select="url/text()"/> 
        </td> 
        <td> 
         <xsl:value-of select="ee/text()"/> 
        </td> 
        <td> 

        </td> 
       </tr> 
      </xsl:for-each> 
     </table> 
     </form> 
    </xsl:template> 
</xsl:stylesheet> 

當我運行它,我總是錯誤消息:

Warning: XSLTProcessor::transformToXml(): runtime error: file file:///D:/includes/xslt/article.xsl line 31 element for-each in D:\includes\bean\articles.php on line 67

Warning: XSLTProcessor::transformToXml(): The 'select' expression does not evaluate to a node set. in D:\includes\bean\articles.php on line 67

但是,當我直接輸入字符串條件:'文章[@key = ' journals/acta/Saxena96 ']'到xslt中,它運行良好。

感謝您的任何建議。

+0

你見過[如何將XPath表達式作爲使用PHP的XSL參數傳遞?](http://stackoverflow.com/q/27774013/367456) – hakre

回答

0

你必須定義在stylesheet級別的參數:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

<xsl:param name="search_condition">defaultValue</xsl:param> 

... 
</xsl:stylesheet> 

你試圖遍歷字符串作爲for-each XPath表達式。 這不起作用。它將被視爲字符串文字。

可能的解決方案是eXSLT dyn:evaluate

+0

親愛的ThW,謝謝你的迴應。你能否更清楚地解釋一下,如何安裝eXSLT dyn:評估?我仍然有importStylesheet()的問題:編譯錯誤。非常感謝。 –

+0

@ minh-hieu.pham:PHP手冊有更多信息:['XSLTProcessor :: hasExsltSupport'](https://php.net/xsltprocessor.hasexsltsupport)和以下使用報告[如何使用XSLTProcessor中的嵌入式EXSLT? ](http://stackoverflow.com/q/1372810/367456)(這不是關於這個問題 - 它提出錯誤 - 但只是在那裏給出的例子) – hakre

相關問題