我想结合使用 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模板网!
Zend 中的动作视图助手 - 解决方法?Action View Helper in Zend - Work around?(Zend 中的动作视图助手 - 解决方法?)
这是将 URI 与 PHP 中用于 MVC 的类/方法匹配的好方Is this a good way to match URI to class/method in PHP for MVC(这是将 URI 与 PHP 中用于 MVC 的类/方法匹配的好方法吗)
我在哪里保存 Zend Framework 中的部分(视图),以便Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?(我在哪里保存 Zend Framework 中的部分(视图),以
有一个网站的单一入口点.坏的?好的?没问题?Having a single entry point to a website. Bad? Good? Non-issue?(有一个网站的单一入口点.坏的?好的?没问题?)
MVC + 服务层在 Zend 或 PHP 中常见吗?Is MVC + Service Layer common in zend or PHP?(MVC + 服务层在 Zend 或 PHP 中常见吗?)
PHP MVC 方法中的 Hello World 示例Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)