XSLファイルがあります。
XSLTProcessorという名前のPHPファイルがあります$bob
。
xsl変換にいくつかのパラメーターを送信したいと思います。
だから、私はこれを私のPHPファイルに書きます。例えば :
$bob->setParameter('', 'message', 'hi');
私のXSLファイルでは、パラメーターを取得するために、次のように記述します。
<xsl:param name="message" />
そして、このパラメーターをXSLに表示したい場合は、次のようにします。
<xsl:value-of select="$message" />
ここに問題があります。
XSLに未定義の数のパラメーターを送信する必要がありますが、その方法がわかりません。私はいくつかの解決策を試しましたが、それらは関連性がありませんでした。たとえば、3つのメッセージをXSLに送信したいのですが、XSLでそれらを使用して次のようなコードを生成したいと思います。
<messages>
<message>Hi</message>
<message>it's bob</message>
<message>How are you ?</message>
</messages>
私のための解決策はありますか?とても素敵になります。私の英語に間違いがあったらごめんなさい。ありがとう、そして良い一日を。
尋ねられたように、これが私が持っているものと私が欲しいものです:
(以下は別です)
これは、posts.xmlという名前の元の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フォームで取得しました。したがって、私の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>