PHP - XSLTProcessor::transformToUri() 函数


定义和用法

XML 是一种在 Web 上共享数据的标记语言,XML 既适用于人类可读,也适用于机器可读。XSL 扩展是 XSL 标准的实现,用于使用 libxslt 库执行 XSTL 转换。

XSLTProcessor::transformToUri() 函数接受类 DOMNode 的对象作为参数,并通过对其应用样式表将其转换为文件 (uri)。

语法


XSLTProcessor::transformToUri($doc, $uri);

参数

参数 描述
doc (必选)这是 DOMNode 类的对象,表示要转换的文档。
uri (必选)这是一个字符串值,表示目标文件的 urii。

返回值

此函数返回一个整数值,该值表示成功时写入的字节数,以及一个布尔值,如果失败,则为 FALSE。

PHP 版本

此功能最初在 PHP 版本 5 中引入,并在所有后续版本中有效。

以下是此函数的示例 -

sample.xml:


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

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="/Qikepu/Title"/>
      Authors: <xsl:apply-templates select="/Qikepu/Authors/Author"/>
   </xsl:template>

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

sample.php:


<?php
   // 加载XSL文档
   $xsl = new DOMDocument();
   $xsl->load("sample.xsl");

   // 加载XML文档
   $xml = new DOMDocument();
   $xml->load("sample.xml");

   // 创建XSLTProcessor
   $proc = new XSLTProcessor();

   // 导入XSL文档
   $proc->importStyleSheet($xsl);

   $file = "output_xstl.html";

   // 将样式转换为XML
   $res = $proc->transformToURI($xml, $file);
   print_r($res);
?>

这将产生以下结果 -

Title - JavaFX Authors: - Krishna – Rajeev