定义和用法
XML 是一种在 Web 上共享数据的标记语言,XML 既适用于人类可读,也适用于机器可读。XSL 扩展是 XSL 标准的实现,用于使用 libxslt 库执行 XSTL 转换。
XSLTProcessor::importStylesheet() 函数接受表示样式表的 DOMDocument 或 SimpleXMLElement 类的对象,并将 givn 样式表导入到当前的 XSTLProcessor 中。
语法
XSLTProcessor::importStylesheet($stylesheet);
参数
参数 | 描述 |
---|---|
stylesheet | (必填)这是类 DOMDocument 或 SimpleXMLElement 的对象,表示您需要导入的样式表。 |
返回值
此函数返回一个布尔值,如果成功,则为 TRUE,如果失败,则为 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);
// 将样式转换为XML
print($proc->transformToXML($xml));
?>
这将产生以下结果 -
Title - JavaFX
Authors:
- Krishna
- Rajeev
Authors:
- Krishna
- Rajeev