これは古いスレッドですが、満足のいく答えはありません。最近、私は同様の状況に直面しましたが、解決策はこのような問題に適用するのに十分一般的であると信じています.
DOMNode
基本的に、PHP と XSLT プロセッサはオブジェクト (パラメーターと戻り値)を介して通信します。したがって、DOMNode
PHP を使用してオブジェクトを構築し、XSLT プロセッサの要求に応じてオブジェクトを返すことができます。
上記の例を考えると、次の XSLT になります。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:php="http://php.net/xsl">
<xsl:template match="/root">
<root>
<xsl:apply-templates select="element" />
</root>
</xsl:template>
<xsl:template match="element">
<element>
<!-- Pass the selected id attributes to a PHP callback,
then literally include the XML as returned from PHP.
Alternatively, one could use xsl:apply-templates
to further transform the result. -->
<xsl:copy-of select="php:function('xslt_callback', @id)" />
</element>
</xsl:template>
</xsl:stylesheet>
また、PHP 関数 (この関数はregisterPHPFunctions
メソッドでエクスポートする必要があります [ php マニュアルを参照]) は次のようになります。
/**
* @param DOMAttr[] $attr_set An array of DOMAttr objects,
* passed by the XSLT processor.
* @return DOMElement The XML to be inserted.
*/
function xslt_callback ($attr_set) {
$id = $attr_set[0]->value;
return new DOMElement('section', $id); //whatever operation you fancy
}
次の XML が生成されます。
<root>
<element>
<section>1</section>
</element>
<element>
<section>2</section>
</element>
</root>
php 関数xslt_callback
は、選択された ID で好きなことを行うことができます。この例では、$attr_set
選択した属性が常に 1 つだけ含まれていると想定しています。状況によっては、範囲または型のチェックを実行することをお勧めします。ただし、これは例のスケルトンを不必要に複雑にするだけです。
注: PHP から単純に XML 文字列を返すと、すべてのおよびに<
および>
タグが挿入されます。<
>