Book/xsl-Phpdoc专题

XSL

目录

简介

类摘要

XSLTProcessor

class XSLTProcessor {

/* 方法 */

public <span class="type">stringfalse <span class="methodname">getParameter ( <span class="type">string $namespace , <span class="methodparam">string $name )

public int <span class="methodname">getSecurityPrefs ( <span class="methodparam">void )

public bool hasExsltSupport ( <span class="methodparam">void )

public bool importStylesheet ( <span class="methodparam">object $stylesheet )

public void registerPHPFunctions ([ <span class="methodparam"><span class="type">arraystring<span class="type">null $functions = null ] )

public bool removeParameter ( <span class="methodparam">string $namespace , string $name )

public bool setParameter ( <span class="methodparam">string $namespace , string $name , <span class="type">string $value )

public bool setProfiling ( <span class="methodparam"><span class="type">stringnull $filename )

public int <span class="methodname">setSecurityPrefs ( <span class="methodparam">int $preferences )

public <span class="type">DOMDocumentfalse transformToDoc ( <span class="methodparam">object $document [, <span class="type">stringnull $returnClass = null ] )

int <span class="methodname">transformToURI ( <span class="methodparam">DOMDocument $doc , string $uri )

public <span class="type">stringfalse<span class="type">null <span class="methodname">transformToXml ( <span class="methodparam">object $document )

}

XSLTProcessor::__construct

Creates a new XSLTProcessor object

说明

XSLTProcessor::__construct ( <span class="methodparam">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

说明

public <span class="type">stringfalse <span class="methodname">XSLTProcessor::getParameter ( <span class="methodparam">string $namespace , string $name )

Gets a parameter if previously set by <span class="function">XSLTProcessor::setParameter.

参数

namespace
The namespace URI of the XSLT parameter.

name
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 <span class="methodname">XSLTProcessor::getSecurityPrefs ( <span class="methodparam">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

说明

public bool XSLTProcessor::hasExsltSupport ( <span class="methodparam">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 ( <span class="methodparam">object $stylesheet )

This method imports the stylesheet into the <span class="classname">XSLTProcessor for transformations.

参数

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

返回值

成功时返回 true, 或者在失败时返回 false

XSLTProcessor::registerPHPFunctions

Enables the ability to use PHP functions as XSLT functions

说明

public void XSLTProcessor::registerPHPFunctions ([ <span class="type">arraystring<span class="type">null $functions = null ] )

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

参数

functions
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);
?>

XSLTProcessor::removeParameter

Remove parameter

说明

public bool XSLTProcessor::removeParameter ( <span class="methodparam">string $namespace , string $name )

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

参数

namespace
The namespace URI of the XSLT parameter.

name
The local name of the XSLT parameter.

返回值

成功时返回 true, 或者在失败时返回 false

参见

  • XSLTProcessor::setParameter
  • XSLTProcessor::getParameter

XSLTProcessor::setParameter

Set value for a parameter

说明

public bool XSLTProcessor::setParameter ( <span class="methodparam">string $namespace , string $name , <span class="type">string $value )

public bool XSLTProcessor::setParameter ( <span class="methodparam">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.

返回值

成功时返回 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

说明

public bool XSLTProcessor::setProfiling ( <span class="methodparam"><span class="type">stringnull $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 <span class="methodname">XSLTProcessor::setSecurityPrefs ( <span class="methodparam">int $preferences )

Sets the security preferences.

参数

preferences
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

说明

public <span class="type">DOMDocumentfalse XSLTProcessor::transformToDoc ( <span class="methodparam">object $document [, <span class="type">stringnull $returnClass = null ] )

Transforms the source node to a <span class="classname">DOMDocument applying the stylesheet given by the XSLTProcessor::importStylesheet method.

参数

document
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 <span class="methodname">XSLTProcessor::transformToURI ( <span class="methodparam">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

说明

public <span class="type">stringfalse<span class="type">null <span class="methodname">XSLTProcessor::transformToXml ( <span class="methodparam">object $document )

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

参数

document
The DOMDocument or <span class="type">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

本站为非盈利网站,作品由网友提供上传,如无意中有侵犯您的版权,请联系删除