定义和用法
XML 是一种在 Web 上共享数据的标记语言,XML 既适用于人类可读,也适用于机器可读。XSL 扩展是 XSL 标准的实现,用于使用 libxslt 库执行 XSTL 转换。
XSLTProcessor::transformToDoc() 函数接受类 DOMNode 的对象作为参数,并通过对其应用样式表将其转换为 DOMDocument。
语法
XSLTProcessor::transformToDoc($doc);
参数
参数 | 描述 |
---|---|
doc | (必选)这是 DOMNode 类的对象,表示要转换的文档。 |
返回值
此函数在成功的情况下返回 DOMDocument 类的对象,如果失败,则返回 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
$res = $proc->transformToDoc($xml);
print_r($res);
?>
这将产生以下结果 -
DOMDocument Object
(
[doctype] =>
[implementation] => (object value omitted)
[documentElement] =>
[actualEncoding] =>
[encoding] =>
[xmlEncoding] =>
[standalone] => 1
[xmlStandalone] => 1
[version] => 1.0
[xmlVersion] => 1.0
[strictErrorChecking] => 1
[documentURI] =>
[config] =>
[formatOutput] =>
[validateOnParse] =>
[resolveExternals] =>
[preserveWhiteSpace] => 1
[recover] =>
[substituteEntities] =>
[nodeName] => #document
[nodeValue] =>
[nodeType] => 9
[parentNode] =>
[childNodes] => (object value omitted)
[firstChild] => (object value omitted)
[lastChild] => (object value omitted)
[previousSibling] =>
[nextSibling] =>
[attributes] =>
[ownerDocument] =>
[namespaceURI] =>
[prefix] =>
[localName] =>
[baseURI] =>
[textContent] =>
Title - JavaFX
Authors:
- Krishna
- Rajeev
)
(
[doctype] =>
[implementation] => (object value omitted)
[documentElement] =>
[actualEncoding] =>
[encoding] =>
[xmlEncoding] =>
[standalone] => 1
[xmlStandalone] => 1
[version] => 1.0
[xmlVersion] => 1.0
[strictErrorChecking] => 1
[documentURI] =>
[config] =>
[formatOutput] =>
[validateOnParse] =>
[resolveExternals] =>
[preserveWhiteSpace] => 1
[recover] =>
[substituteEntities] =>
[nodeName] => #document
[nodeValue] =>
[nodeType] => 9
[parentNode] =>
[childNodes] => (object value omitted)
[firstChild] => (object value omitted)
[lastChild] => (object value omitted)
[previousSibling] =>
[nextSibling] =>
[attributes] =>
[ownerDocument] =>
[namespaceURI] =>
[prefix] =>
[localName] =>
[baseURI] =>
[textContent] =>
Title - JavaFX
Authors:
- Krishna
- Rajeev
)