PHP - XSLTProcessor::setParameter() 函式



定義和用法

XML 是一種用於在網路上共享資料的標記語言,XML 既可供人類閱讀,也可供機器閱讀。XSL 擴充套件是 XSL 標準的實現,用於使用 libxslt 庫執行 XSTL 轉換。

XSLTProcessor::setParameter() 函式用於設定當前轉換的引數值。

語法

XSLTProcessor::setParameter($namespace, $name, $value);

引數

序號 引數及描述
1

名稱空間(必填)

這是一個字串值,表示 XSLT 引數的 URI。

2

名稱(必填)

這是一個字串值,表示 XSLT 引數的名稱。

3

值(必填)

這是一個字串,表示 XSLT 引數的值。

返回值

此函式返回一個布林值,成功時為 TRUE,失敗時為 FALSE。

PHP 版本

此函式首次引入於 PHP 5 版本,並在所有後續版本中均有效。

示例

以下是此函式的示例:

sample.xml

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="example.xsl"?>
<Tutorial>
   <Title>JavaFX</Title>
   <Authors>
      <Author>Krishna</Author>
      <Author>Rajeev</Author>
   </Authors>
   <Body>Sample text</Body>
</Tutorial>

sample.xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="text"/>

   <xsl:template match="/">
      Title - <xsl:value-of select="/Tutorial/Title"/>
      Authors: <xsl:apply-templates select="/Tutorial/Authors/Author"/>
   </xsl:template>

   <xsl:template match="Author">
      - <xsl:value-of select="." />
   </xsl:template>
</xsl:stylesheet>

sample.php

<?php
   //Loading an XSL document
   $xsl = new DOMDocument();
   $xsl->load("sample.xsl");

   //Loading an XML document
   $xml = new DOMDocument();
   $xml->load("sample.xml");

   //Creating an XSLTProcessor
   $proc = new XSLTProcessor();

   //Importing the XSL document
   $proc->importStyleSheet($xsl);

   //Setting parameter
   $proc->setParameter('', 'param', 'test_value');
   $val = $proc->getParameter('', 'param');

   print("Parameter Value :".$val);

   //Transforming the style to XML
   print($proc->transformToXML($xml));
   //print(res);
?>

這將產生以下結果:

Parameter Value :test_value
   Title - JavaFX
   Authors:
   - Krishna
   - Rajeev
php_function_reference.htm
廣告