我是XML / XSL的新手。我希望能够在规则字符串中传递var并返回正确的数据。
现在我有了这个PHP:
<?php
$params = array('id' => $_GET['id']);
$xslDoc = new DOMDocument();
$xslDoc->load("test.xsl");
$xmlDoc = new DOMDocument();
$xmlDoc->load("test.xml");
$xsltProcessor = new XSLTProcessor();
$xsltProcessor->registerPHPFunctions();
$xsltProcessor->importStyleSheet($xslDoc);
foreach ($params as $key => $val)
$xsltProcessor->setParameter('', $key, $val);
echo $xsltProcessor->transformToXML($xmlDoc);
?>
我的xml文件如下所示:
<Profiles>
<Profile>
<id>1</id>
<name>john doe</name>
<dob>188677800</dob>
</Profile>
<Profile>
<id>2</id>
<name>mark antony</name>
<dob>79900200</dob>
</Profile>
<Profile>
<id>3</id>
<name>neo anderson</name>
<dob>240431400</dob>
</Profile>
<Profile>
<id>4</id>
<name>mark twain</name>
<dob>340431400</dob>
</Profile>
<Profile>
<id>5</id>
<name>frank hardy</name>
<dob>390431400</dob>
</Profile>
</Profiles>
我的xsl看起来像这样
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="id" />
<xsl:template match="*">
<html><body>
<h2>Profile</h2>
<table cellspacing="1" cellpadding="5" border="1">
<caption>User Profiles</caption>
<tr><th>ID</th><th>Name</th><th>Date of Birth</th></tr>
<xsl:for-each select="/Profiles/Profile[id='$id']">
<tr>
<td><xsl:value-of select="id"/></td>
<td><xsl:value-of select="php:function('ucwords', string(name))"/></td>
<td><xsl:value-of select="php:function('date', 'jS M, Y', number(dob))"/></td>
</tr>
</xsl:for-each>
</table>
</body></html>
</xsl:template>
</xsl:stylesheet>
当我像这样测试网址时:
http://foo.com/sanbox/index.php?id=2
我只得到:
Profile User Profiles ID Name Date of Birth.
答案 0 :(得分:7)
错误是因为您没有包含正确的命名空间。
在你的xsl:stylesheet声明中包含xmlns:php =“http://php.net/xsl”
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:php="http://php.net/xsl"
>
答案 1 :(得分:3)
在XPATH表达式中,变量名$ id周围不应该有任何qoutes,所以它应该是:
<xsl:for-each select="/Profiles/Profile[id=$id]">
此外,您可以在文档中放置<xsl:value-of select="$id"/>
以确保值传递。
答案 2 :(得分:1)
嘿,我也是实现xsl / xml的新手,但我玩了一段代码,我无法让它工作但即使你改变了
<xsl:for-each select="/Profiles/Profile[id='$id']">
到
<xsl:for-each select="/Profiles/Profile[id='2']">
虽然它确实可以获取正确的信息,但您仍然会收到一个令人讨厌的错误。如果删除任何“”甚至“”除了数字以外的任何内容都会产生更多错误。虽然我没有时间测试它,但我找到了另一种方法。 Client Side XSLT
我遇到了和你一样的问题,并希望看到这个问题得到解决。
警告:XSLTProcessor :: transformToXml()[xsltprocessor.transformtoxml]:xmlXPathCompOpEval:绑定到第17行E:\ xampplite \ htdocs \ XSL \ index.php中未定义前缀php的函数函数
警告:XSLTProcessor :: transformToXml()[xsltprocessor.transformtoxml]:xmlXPathCompiledEval:堆栈上剩下1个对象。在第17行的E:\ xampplite \ htdocs \ XSL \ index.php
警告:XSLTProcessor :: transformToXml()[xsltprocessor.transformtoxml]:xmlXPathCompOpEval:绑定到第17行E:\ xampplite \ htdocs \ XSL \ index.php中未定义前缀php的函数函数
警告:XSLTProcessor :: transformToXml()[xsltprocessor.transformtoxml]:xmlXPathCompiledEval:堆栈上剩下2个对象。在第17行的E:\ xampplite \ htdocs \ XSL \ index.php
资料
用户个人资料 ID名称出生日期 2标记antony 79900200
答案 3 :(得分:0)
这里有一些问题阻止它开始工作,有些已经提到过......
如上所述的命名空间:
<stylesheet xmlns:php="http://php.net/xsl"
for-each ...我倾向于使用:
<xsl:template match="/">
然后
<xsl:for-each select="Profiles/Profile[id=$id]">
此外,报价不正确,如上所述,ID不需要&#39;
通过这三项更改,它可以运作....