PHP - XSLTProcessor::importStylesheet() 函式



定義和用法

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

XSLTProcessor::importStylesheet() 函式接受一個 DOMDocument 或 SimpleXMLElement 類的物件,該物件表示樣式表,並將給定的樣式表匯入到當前的 XSTLProcessor 中。

語法

XSLTProcessor::importStylesheet($stylesheet);

引數

序號 引數和描述
1

styesheet (必填)

這是一個 DOMDocument 或 SimpleXMLElement 類的物件,表示您需要匯入的樣式表。

返回值

此函式返回一個布林值,如果成功則為 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);

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

這將產生以下結果:

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