将变量传递给 XSLT [英] Passing variables to XSLT
本文介绍了将变量传递给 XSLT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 XML/XSL 的新手.我希望能够在规则字符串中传递一个 var 并让它返回正确的数据.
现在我有这个 PHP:
$_GET['id']);$xslDoc = new DOMDocument();$xslDoc->load("test.xsl");$xmlDoc = 新的 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 文件如下所示:
<简介><id>1</id><name>john doe</name><dob>188677800</dob></个人资料><简介><id>2</id><name>标记安东尼</name><dob>79900200</dob></个人资料><简介><id>3</id><name>neo anderson</name><dob>240431400</dob></个人资料><简介><id>4</id><name>mark twain</name><dob>340431400</dob></个人资料><简介><id>5</id><姓名>弗兰克·哈代</姓名><dob>390431400</dob></个人资料></个人资料>
我的 xsl 看起来像这样
<xsl:param name="id"/><xsl:template match="*"><html><body><h2>个人资料</h2><table cellpacing="1" cellpadding="5" border="1"><caption>用户配置文件</caption><tr><th>ID</th><th>姓名</th><th>出生日期</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></body></html></xsl:模板></xsl:stylesheet>
当我像这样测试网址时:
http://foo.com/sanbox/index.php?id=2
我只得到:
<前>轮廓用户配置文件 ID 名称 出生日期. 解决方案
错误是因为您没有包含正确的命名空间.
在您的 xsl:stylesheet 声明中包含 xmlns:php="http://php.net/xsl"
I am new to XML/XSL. I want to be able to pass a var in a rule string and have that return the correct data.
Right now I have have this 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);
?>
My xml file looks like this:
<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>
And my xsl looks like this
<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>
When I test the url like this:
http://foo.com/sanbox/index.php?id=2
I only get:
Profile User Profiles ID Name Date of Birth.
解决方案
The error is because you havent included the correct namespace.
In your xsl:stylesheet declaration include 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"
>
这篇关于将变量传递给 XSLT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文