【发布时间】:2012-11-04 13:22:07
【问题描述】:
我有一个 XSL 文件。
我有一个带有名为$bob 的 XSLTProcessor 的 PHP 文件。
我想向我的 xsl 转换发送一些参数。
所以,我把它写在我的 PHP 文件中;例如:
$bob->setParameter('', 'message', 'hi');
在我的 XSL 文件中,要获取参数,我会这样写:
<xsl:param name="message" />
如果我想在我的 XSL 中显示这个参数,我会这样做:
<xsl:value-of select="$message" />
问题来了。
我必须向我的 XSL 发送未定义数量的参数,但我不知道该怎么做。我尝试了几种解决方案,但它们不相关。例如,我想向我的 XSL 发送 3 条消息,并且我希望我的 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>
这是我想在 final 中使用的 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>
【问题讨论】:
-
您转换的原始 XML 是什么?你的 XSL XML 是什么?您只有一小部分可以很好地解释您的问题,但是,由于
setParameter只允许为每个名称设置一个值,因此不清楚您要做什么。原始的 XML 和 XSL 会更清楚一些。 -
user1786510:您可能对比使用竖线分隔的字符串更灵活和可扩展的解决方案感兴趣。
标签: php xslt parameters