使用setParameter在PHP变量中解析为XSLT

时间:2013-03-02 10:12:58

标签: php xml xslt

我有这个xml文件

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>

<bookstore>

<book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>

<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

<book category="WEB">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book>

<book category="WEB">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
  <price>39.95</price>
</book>

</bookstore>
我有这个PHP代码

<?php
$xslDoc = new DOMDocument();
$xslDoc->load("test.xsl");

$xmlDoc = new DOMDocument();
$xmlDoc->load("test.xml");

$xsltProcessor = new XSLTProcessor();
$xsltProcessor->registerPHPFunctions();
$xsltProcessor->importStyleSheet($xslDoc);
$xsltProcessor->setParameter('','s_name', 'title');
$xsltProcessor->setParameter('','s_value', 35);

echo $xsltProcessor->transformToXML($xmlDoc);
?>

这是我的XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl" xmlns:str="http://exslt.org/strings"  exclude-result-prefixes="str">

<xsl:template match="/">
  <html>
  <body>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th>O/p</th>
    </tr>
    <xsl:for-each select="/bookstore/book[price>$s_value]/$s_name">
    <tr>
      <td><xsl:value-of select="."/></td>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

这里我想向我的xsl转换发送一些参数。这是s_names_value

这给了我错误

如果我只使用s_value那么它的工作正常n xsl将是

<xsl:for-each select="/bookstore/book[price>$s_value]/title">

如果我使用它们那么它不起作用

1 个答案:

答案 0 :(得分:2)

这是无效的XPath:

/bookstore/book[price>$s_value]/$s_name

因为在像这样的斜杠之后不能发生变量。为了完成你想要做的事,试试这个:

/bookstore/book[price>$s_value]/*[local-name() = $s_name]