PHP: XSL functions - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<xdiff_string_patchxsl_xsltprocessor_get_parameter>
view the version of this page
Last updated: Tue, 21 Dec 2004

CXXX. XSL functions

導入

The XSL extension implements the XSL standard, performing XSLT transformations using the

要件

This extension uses libxslt which can be found at . libxslt version 1.0.18 or greater is required.

インストール手順

PHP 5 includes the XSL extension by default and can be enabled by adding the argument --with-xsl[=DIR] to your configure line. DIR is the libxslt installation directory.

In this small tutorial we will learn how to transform an XML document into HTML.

例 1. A simple XSL tree

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html" encoding="iso-8859-1" indent="no"/>
 <xsl:template match="collection">
  Hey! Welcome to my sweet CD collection!
  <xsl:apply-templates/>
 </xsl:template>
 <xsl:template match="cd">
  <h1><xsl:value-of select="title"/></h1>
  <h2>by <xsl:value-of select="artist"/></h2>
  <h3> - <xsl:value-of select="year"/></h3>
 </xsl:template>
</xsl:stylesheet>

例 2. Corresponding XML tree

<collection>
 <cd>
  <title>PHP Rock</title>
  <artist>Joe Coder</artist>
  <year>2003</year>
 </cd>
 <cd>
  <title>Squashing Typos on a Winter's Eve</title>
  <artist>kennyt</artist>
  <year>2004</year>
 </cd>
</collection>

例 3. Making XML into HTML

The following PHP code uses the XML and XSL extensions to transform XML into presentable HTML.

<?php
/* Load the two XML sources */
$xml = new DomDocument; // from /ext/dom
$xml->load('example.xml');

$xsl = new DomDocument;
$xsl->load('example.xsl');

/* Configure the transformer */
$proc = new xsltprocessor;
$proc->importStyleSheet($xsl); // attach the xsl rules
echo $proc->transformToXML($xml); // actual transformation
?>

This should produce an HTML fragment similar to the following:

Hey! Welcome to my sweet CD collection!

<h1>PHP Rock</h1>
<h2>by Joe Coder</h2>
<h3> - 2003</h3>

<h1>Squashing Typos on a Winter's Eve</h1>
<h2> by kennyt</h2>
<h3> - 2004</h3>

定義済みの定数

これらの定数は、この拡張モジュールで定義されており、 この拡張モジュールがPHP内部にコンパイルされているか実行時に動的にロー ドされるかのどちらかの場合のみ使用可能です。

XSL_CLONE_AUTO (integer)

XSL_CLONE_NEVER (integer)

XSL_CLONE_ALWAYS (integer)

目次
xsl_xsltprocessor_get_parameter -- Get value of a parameter
xsl_xsltprocessor_has_exslt_support -- Determine if PHP has EXSLT support
xsl_xsltprocessor_import_stylesheet -- Import stylesheet
xsl_xsltprocessor_register_php_functions -- Enables the ability to use PHP functions as XSLT functions
xsl_xsltprocessor_remove_parameter -- Remove parameter
xsl_xsltprocessor_set_parameter -- Set value for a parameter
xsl_xsltprocessor_transform_to_doc -- Transform to document
xsl_xsltprocessor_transform_to_uri -- Transform to URI
xsl_xsltprocessor_transform_to_xml -- Transform to XML


add a note add a note User Contributed Notes
XSL functions
venkatesh at lammersmedical dot com
01-Mar-2005 02:17
Here is function to read from the XSL sheet which is saved as a text file.

function ReadExcelSheet($filename){
             $test=file($filename);
             $ar1=str_replace("~[^\t]*\t","\t",$test);
             $ar2=str_replace("~","",$ar1);
             $ar=str_replace("�","",$ar2);
             $temp=array();
             for ($i=0; $i<count($ar); $i++) {
                       if((substr($ar[$i],0,1)!= "\t")){
                           if($ar[$i]!=="\r\n"){
                           array_push($temp,$ar[$i]);
           }
       }
   }
   $name=split("\t",$temp[0]);
   $ExcelList=array();
   for($i=1;$i<count($temp);$i++){
       $split_result=split("\t",$temp[$i]);
       array_push($ExcelList,$split_result);
   }
   $result=insert_into_array($ExcelList,0,$name);
   return($result);
}
abel at aberen dot com
22-Feb-2005 03:19
When migrating from sablotron to this library it's worth the use of the wrapper shared by jw at jwscripts dot com above.
Anyway I found more cumbersome to adjust the xsl code since sablotron is more tolerant and less strict than libxslt so chances are that  libxslt complains of your code. In such case try to find the offending code and stick to the standard.
appletalk at gmail dot com
05-Feb-2005 06:32
As many of you may have noticed, DOM parser gives errors if the '&nbsp;' entity is present. The E_WARN message looks like:

Warning: DOMDocument::load() [function.load]: Entity 'nbsp' not defined in ...

There're many ways to solve this:
a) The hard way
<xsl:text disable-output-escaping="yes"> &amp;nbsp;</xsl:text>

b) Defining &nbsp;
At the top of the document, after the <?xml?> definition, add:
   <!DOCTYPE xsl:stylesheet [
   <!ENTITY nbsp "&#160;" >
   ]>

c) External Doctype
Just in case you want need other HTML entities, you can call an external doctype with the proper definitions

<!DOCTYPE page SYSTEM "">

Of course, you can download the file and place it in your server.
kekoajs at yahoo dot com
22-Dec-2004 10:07
Wow, I spent the better part of a day looking for how one could pass an entire test expression to an XSL stylesheet.  It seems that the XSLT 1.0 specification doesn't support it but PHP 5 (and maybe 4s) inclusion of  EXSLT allows one to do exactly that...

simply add these lines...

xmlns:dyn=""
extension-element-prefixes="dyn"

to the <xsl:stylesheet> element and when using an expression stored in a <xsl:param> element write

<xsl:if test="dyn:evaluate($param-name)">

and viola!  you can now use expressions generated externally in your stylesheet!

EXSLT adds many useful functions that can be integrated into your XSL in a similar fashion.  You can go to to learn more...
rd at jerntorget dot se
24-Sep-2004 10:58
If you get the following warning message:

xsltApplyOneTemplate: _if_ was not compiled in (if can for example be apply-template or some other method), i've found that the problem seems to be an &nbsp; directly before the <xsl:if> or what ever is causing the problem.

One way to get thru the problem is to use span tags around &nbsp;  like : <span>&nbsp;</span>
jw at jwscripts dot com
06-Sep-2004 09:23
The following code is a wrapper to support calls to some of the old xslt_* functions:

<?

if (PHP_VERSION >= 5) {
  
// Emulate the old xslt library functions
  
function xslt_create() {
       return new
XsltProcessor();
   }

   function
xslt_process($xsltproc,
                        
$xml_arg,
                        
$xsl_arg,
                        
$xslcontainer = null,
                        
$args = null,
                        
$params = null) {
      
// Start with preparing the arguments
      
$xml_arg = str_replace('arg:', '', $xml_arg);
      
$xsl_arg = str_replace('arg:', '', $xsl_arg);

      
// Create instances of the DomDocument class
      
$xml = new DomDocument;
      
$xsl = new DomDocument;

      
// Load the xml document and the xsl template
      
$xml->loadXML($args[$xml_arg]);
      
$xsl->loadXML($args[$xsl_arg]);

      
// Load the xsl template
      
$xsltproc->importStyleSheet($xsl);

      
// Set parameters when defined
      
if ($params) {
           foreach (
$params as $param => $value) {
              
$xsltproc->setParameter("", $param, $value);
           }
       }

      
// Start the transformation
      
$processed = $xsltproc->transformToXML($xml);

      
// Put the result in a file when specified
      
if ($xslcontainer) {
           return @
file_put_contents($xslcontainer, $processed);
       } else {
           return
$processed;
       }

   }

   function
xslt_free($xsltproc) {
       unset(
$xsltproc);
   }
}

$arguments = array(
  
'/_xml' => file_get_contents("newxslt.xml"),
  
'/_xsl' => file_get_contents("newxslt.xslt")
);

$xsltproc = xslt_create();
$html = xslt_process(
  
$xsltproc,
  
'arg:/_xml',
  
'arg:/_xsl',
  
null,
  
$arguments
);

xslt_free($xsltproc);
print
$html;

?>
rojaro
24-Jun-2004 02:43
If you're want to use XML from a variable e.g. $xmldata but the XSLT-StyleSheet from a file, you can do it also the following way:

<?php
$xslt
= new xsltProcessor;
$xslt->importStyleSheet(DomDocument::load('filename.xsl'));
print
$xslt->transformToXML(DomDocument::loadXML($xmldata));
?>
tino at infeon dot com
06-May-2004 08:46
when compiling libxslt point out where your libxml was installed to

./configure --prefix=/usr/local --with-libxml-prefix=/usr/local

this will prevent errors when configuring libxslt

<xdiff_string_patchxsl_xsltprocessor_get_parameter>
 Last updated: Tue, 21 Dec 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: /
Last updated: Mon Mar 14 08:13:06 2005 Local time zone must be set--see zic manual page