PHP 可以与 XSLT 通信吗?

Can PHP communicate with XSLT?(PHP 可以与 XSLT 通信吗?)
本文介绍了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 文件:

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to make 5 random numbers with sum of 100(如何制作5个总和为100的随机数)
str_shuffle and randomness(str_shuffle 和随机性)
Algorithm for generating a random number(生成随机数的算法)
What#39;s the disadvantage of mt_rand?(mt_rand 的缺点是什么?)
What is the best way to generate a random key within PHP?(在 PHP 中生成随机密钥的最佳方法是什么?)
How to create a random string using PHP?(如何使用 PHP 创建随机字符串?)