XSL

目录

简介

类摘要

XSLTProcessor

class XSLTProcessor {

/* 方法 */

string getParameter ( string $namespaceURI , string $localName )

public int getSecurityPrefs ( void )

bool hasExsltSupport ( void )

public bool importStylesheet ( object $stylesheet )

void registerPHPFunctions ([ mixed $restrict ] )

bool removeParameter ( string $namespaceURI , string $localName )

bool setParameter ( string $namespace , string $name , string $value )

bool setProfiling ( string $filename )

public int setSecurityPrefs ( int $securityPrefs )

DOMDocument transformToDoc ( DOMNode $doc )

int transformToURI ( DOMDocument $doc , string $uri )

string transformToXml ( object $doc )

}

XSLTProcessor::__construct

Creates a new XSLTProcessor object

说明

XSLTProcessor::__construct ( void )

Creates a new XSLTProcessor object.

参数

此函数没有参数。

返回值

没有返回值。

范例

示例 #1 Creating an XSLTProcessor

<?php

$xsldoc = new DOMDocument();
$xsldoc->load($xsl_filename);

$xmldoc = new DOMDocument();
$xmldoc->load($xml_filename);

$xsl = new XSLTProcessor();
$xsl->importStyleSheet($xsldoc);
echo $xsl->transformToXML($xmldoc);

?>

XSLTProcessor::getParameter

Get value of a parameter

说明

string XSLTProcessor::getParameter ( string $namespaceURI , string $localName )

Gets a parameter if previously set by XSLTProcessor::setParameter.

参数

namespaceURI
The namespace URI of the XSLT parameter.

localName
The local name of the XSLT parameter.

返回值

The value of the parameter (as a string), or FALSE if it's not set.

参见

  • XSLTProcessor::setParameter
  • XSLTProcessor::removeParameter

XSLTProcessor::getSecurityPrefs

Get security preferences

说明

public int XSLTProcessor::getSecurityPrefs ( void )

Gets the security preferences.

参数

此函数没有参数。

返回值

A bitmask consisting of XSL_SECPREF_READ_FILE, XSL_SECPREF_WRITE_FILE, XSL_SECPREF_CREATE_DIRECTORY, XSL_SECPREF_READ_NETWORK, XSL_SECPREF_WRITE_NETWORK.

XSLTProcessor::hasExsltSupport

Determine if PHP has EXSLT support

说明

bool XSLTProcessor::hasExsltSupport ( void )

This method determines if PHP was built with the » EXSLT library.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Testing EXSLT support

<?php

$proc = new XSLTProcessor;
if (!$proc->hasExsltSupport()) {
    die('EXSLT support not available');
}

// do EXSLT stuff here ..

?>

XSLTProcessor::importStylesheet

Import stylesheet

说明

public bool XSLTProcessor::importStylesheet ( object $stylesheet )

This method imports the stylesheet into the XSLTProcessor for transformations.

参数

stylesheet
The imported style sheet as a DOMDocument or SimpleXMLElement object.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

更新日志

版本说明
5.2.8Accepts SimpleXMLElement again which was broken since PHP 5.2.6.

XSLTProcessor::registerPHPFunctions

Enables the ability to use PHP functions as XSLT functions

说明

void XSLTProcessor::registerPHPFunctions ([ mixed $restrict ] )

This method enables the ability to use PHP functions as XSLT functions within XSL stylesheets.

参数

restrict
Use this parameter to only allow certain functions to be called from XSLT.

This parameter can be either a string (a function name) or an array of functions.

返回值

没有返回值。

范例

示例 #1 Simple PHP Function call from a stylesheet

<?php
$xml = <<<EOB
<allusers>
 <user>
  <uid>bob</uid>
 </user>
 <user>
  <uid>joe</uid>
 </user>
</allusers>
EOB;
$xsl = <<<EOB
<?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">
<xsl:output method="html" encoding="utf-8" indent="yes"/>
 <xsl:template match="allusers">
  <html><body>
    <h2>Users</h2>
    <table>
    <xsl:for-each select="user">
      <tr><td>
        <xsl:value-of
             select="php:function('ucfirst',string(uid))"/>
      </td></tr>
    </xsl:for-each>
    </table>
  </body></html>
 </xsl:template>
</xsl:stylesheet>
EOB;
$xmldoc = DOMDocument::loadXML($xml);
$xsldoc = DOMDocument::loadXML($xsl);

$proc = new XSLTProcessor();
$proc->registerPHPFunctions();
$proc->importStyleSheet($xsldoc);
echo $proc->transformToXML($xmldoc);
?>

更新日志

版本说明
5.1.0The restrict parameter was added.

XSLTProcessor::removeParameter

Remove parameter

说明

bool XSLTProcessor::removeParameter ( string $namespaceURI , string $localName )

Removes a parameter, if set. This will make the processor use the default value for the parameter as specified in the stylesheet.

参数

namespaceURI
The namespace URI of the XSLT parameter.

localName
The local name of the XSLT parameter.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

参见

  • XSLTProcessor::setParameter
  • XSLTProcessor::getParameter

XSLTProcessor::setParameter

Set value for a parameter

说明

bool XSLTProcessor::setParameter ( string $namespace , string $name , string $value )

bool XSLTProcessor::setParameter ( string $namespace , array $options )

Sets the value of one or more parameters to be used in subsequent transformations with XSLTProcessor. If the parameter doesn't exist in the stylesheet it will be ignored.

参数

namespace
The namespace URI of the XSLT parameter.

name
The local name of the XSLT parameter.

value
The new value of the XSLT parameter.

options
An array of name => value pairs. This syntax is available since PHP 5.1.0.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Changing the owner before the transformation

<?php

$collections = array(
    'Marc Rutkowski' => 'marc',
    'Olivier Parmentier' => 'olivier'
);

$xsl = new DOMDocument;
$xsl->load('collection.xsl');

// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules

foreach ($collections as $name => $file) {
    // Load the XML source
    $xml = new DOMDocument;
    $xml->load('collection_' . $file . '.xml');

    $proc->setParameter('', 'owner', $name);
    $proc->transformToURI($xml, 'file:///tmp/' . $file . '.html');
}

?>

参见

  • XSLTProcessor::getParameter
  • XSLTProcessor::removeParameter

XSLTProcessor::setProfiling

Sets profiling output file

说明

bool XSLTProcessor::setProfiling ( string $filename )

Sets the file to output profiling information when processing a stylesheet.

参数

filename
Path to the file to dump profiling information.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Example profiling output

<?php
// Load the XML source
$xml = new DOMDocument;
$xml->load('collection.xml');

$xsl = new DOMDocument;
$xsl->load('collection.xsl');

// Configure the transformer
$proc = new XSLTProcessor;
$proc->setProfiling('profiling.txt');
$proc->importStyleSheet($xsl); // attach the xsl rules

echo trim($proc->transformToDoc($xml)->firstChild->wholeText);
?>

The above code will produce the following information in the profiling file:

number               match                name      mode  Calls Tot 100us Avg

    0                   cd                                    2      3      1
    1           collection                                    1      1      1

                         Total                                3      4

XSLTProcessor::setSecurityPrefs

Set security preferences

说明

public int XSLTProcessor::setSecurityPrefs ( int $securityPrefs )

Sets the security preferences.

参数

securityPrefs
The new security preferences. The following constants can be ORed: XSL_SECPREF_READ_FILE, XSL_SECPREF_WRITE_FILE, XSL_SECPREF_CREATE_DIRECTORY, XSL_SECPREF_READ_NETWORK, XSL_SECPREF_WRITE_NETWORK. Alternatively, XSL_SECPREF_NONE or XSL_SECPREF_DEFAULT can be passed.

返回值

Returns the old security preferences.

XSLTProcessor::transformToDoc

Transform to a DOMDocument

说明

DOMDocument XSLTProcessor::transformToDoc ( DOMNode $doc )

Transforms the source node to a DOMDocument applying the stylesheet given by the XSLTProcessor::importStylesheet method.

参数

doc
The node to be transformed.

返回值

The resulting DOMDocument or FALSE on error.

范例

示例 #1 Transforming to a DOMDocument

<?php

// Load the XML source
$xml = new DOMDocument;
$xml->load('collection.xml');

$xsl = new DOMDocument;
$xsl->load('collection.xsl');

// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules

echo trim($proc->transformToDoc($xml)->firstChild->wholeText);

?>

以上例程会输出:

Hey! Welcome to Nicolas Eliaszewicz's sweet CD collection!

参见

  • XSLTProcessor::transformToUri
  • XSLTProcessor::transformToXml

XSLTProcessor::transformToUri

Transform to URI

说明

int XSLTProcessor::transformToURI ( DOMDocument $doc , string $uri )

Transforms the source node to an URI applying the stylesheet given by the XSLTProcessor::importStylesheet method.

参数

doc
The document to transform.

uri
The target URI for the transformation.

返回值

Returns the number of bytes written or FALSE if an error occurred.

范例

示例 #1 Transforming to a HTML file

<?php

// Load the XML source
$xml = new DOMDocument;
$xml->load('collection.xml');

$xsl = new DOMDocument;
$xsl->load('collection.xsl');

// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules

$proc->transformToURI($xml, 'file:///tmp/out.html');

?>

参见

  • XSLTProcessor::transformToDoc
  • XSLTProcessor::transformToXml

XSLTProcessor::transformToXml

Transform to XML

说明

string XSLTProcessor::transformToXml ( object $doc )

Transforms the source node to a string applying the stylesheet given by the xsltprocessor::importStylesheet method.

参数

doc
The DOMDocument or SimpleXMLElement object to be transformed.

返回值

The result of the transformation as a string or FALSE on error.

范例

示例 #1 Transforming to a string

<?php

// Load the XML source
$xml = new DOMDocument;
$xml->load('collection.xml');

$xsl = new DOMDocument;
$xsl->load('collection.xsl');

// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules

echo $proc->transformToXML($xml);

?>

以上例程会输出:

Hey! Welcome to Nicolas Eliaszewicz's sweet CD collection!

<h1>Fight for your mind</h1><h2>by Ben Harper - 1995</h2><hr>
<h1>Electric Ladyland</h1><h2>by Jimi Hendrix - 1997</h2><hr>

参见

  • XSLTProcessor::transformToDoc
  • XSLTProcessor::transformToUri