PHP可以与XSLT通信吗? [英] Can PHP communicate with XSLT?

查看:64
本文介绍了PHP可以与XSLT通信吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想结合使用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文件:

<?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通信吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆