問題描述
我想結合使用 xml &xslt 作為模板系統.我想回答的問題是:xslt 和 PHP 可以互相通信嗎(即共享變量)?
I want to use a combination of xml & xslt as a templating system. The question that I want answered is: can xslt and PHP communicate with each other (i.e share variables)?
推薦答案
您可以使用 PHP 完成的基本任務是定義要使用哪個 XSLT 腳本轉換哪個 XML 文件.使用這個你可以
a) 將參數從 PHP 傳遞到 XSLT 和
b) 在 XSLT 腳本中使用 PHP 函數.
這個例子展示了如何 - 第一個 PHP 文件:
The basic task you can do with PHP is to define which XML file to transform with which XSLT script. Using this you can
a) pass parameters from PHP to XSLT and
b) use PHP functions in the XSLT script.
This example shows how - first PHP file:
<?php
function f($value){
//do something
return $value;
}
$proc=new XsltProcessor;
$proc->registerPHPFunctions();
$proc->setParameter('', 'p', '123');
$proc->importStylesheet(DOMDocument::load("script.xsl"));
echo $proc->transformToXML(DOMDocument::load("data.xml"));
?>
第二個 XSLT 文件:
second XSLT file:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl" exclude-result-prefixes="php">
<xsl:param name="p" select="''"/>
<xsl:template match="/">
<xsl:value-of select="$p"/>
<xsl:value-of select="php:function('f', '456')"/>
</xsl:template>
</xsl:stylesheet>
輸出應該是 123456
select="''" 而不是 select=""
The output should be 123456
EDITED: select="''" instead select=""
這篇關于PHP 可以與 XSLT 通信嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!