我有一个XSL文件。
我有一个XSLTProcessor中命名的PHP文件$bob
。
我想送我的XSL转换的一些参数。
所以,在我的PHP文件中写入这一点; 例如 :
$bob->setParameter('', 'message', 'hi');
在我的XSL文件,以获取参数,我写这篇文章,例如:
<xsl:param name="message" />
如果我想在我的XSL来显示这个PARAM,我这样做:
<xsl:value-of select="$message" />
这里谈到的问题。
我要送我的XSL的参数未定义的号码,我不知道该怎么做。 我尝试了几种解决方案,但他们是不相关的。 我想3条消息发送,例如我的XSL,我希望我的XSL使用它们来产生这样的代码:
<messages>
<message>Hi</message>
<message>it's bob</message>
<message>How are you ?</message>
</messages>
你有我一个解决方案? 这将是非常好的。 很抱歉,如果我的英语错误。 谢谢你,有一个美好的一天。
至于问,这里是我有什么,我想有 :
(下文中被分离)
这是我原来的XML命名posts.xml的简化版本:
<posts>
<post id="post1" >
<titre>Hey</titre>
<motscles>
<motcle>Batman</motcle>
<motcle>Cats</motcle>
</motscles>
</posts>
</posts>
这是我想在最后的XML:
<posts>
<post id="post1" >
<titre>Hey</titre>
<motscles>
<motcle>Batman</motcle>
<motcle>Cats</motcle>
</motscles>
</posts>
<post id="post2" >
<titre>Toto</titre>
<motscles>
<motcle>Superman</motcle>
<motcle>Dogs</motcle>
<motcle>Cake</motcle>
</motscles>
</posts>
</posts>
我通过HTML形式获得交(滴度,motscles)的信息。 所以,我的PHP文件获取信息,并将其发送给我的XSL文件:
// initialize xml and xsl
$xml = new DOMDocument();
$xml->load('posts.xml');
$xsl = new DOMDocument();
$xsl->load('addpost.xsl');
// Initialize the XSLTProcessor
$addPost = new XSLTProcessor();
$addPost->importStylesheet($xsl);
// Define parameters
$addPost->setParameter('', 'titre', $_POST['titre']);
// Get the modified xml
$xml = $addPost->transformToDoc($xml);
// Save the modified xml
$xml->save('posts.xml');
这里是我的XSL的简化版本:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output
method="xml"
indent="yes"
encoding="UTF-8"
/>
<xsl:param name="titre" />
<xsl:param name="motscles" />
<xsl:template match="posts" >
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="@*|node()"/>
<xsl:call-template name="post" />
</xsl:copy>
</xsl:template>
<!-- Template de post -->
<xsl:template name="post" >
<post id="{$id}" >
<titre><xsl:value-of select="$titre" /></titre>
<motscles>
</motscles>
</post>
</xsl:template>
<!-- Copier les nodes et attributs récursivement -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>