Примеры
Many examples in this reference require both an XML and an XSL file. We will use collection.xml and collection.xsl that contains the following:
Пример #1 collection.xml
<collection> <cd> <title>Fight for your mind</title> <artist>Ben Harper</artist> <year>1995</year> </cd> <cd> <title>Electric Ladyland</title> <artist>Jimi Hendrix</artist> <year>1997</year> </cd> </collection>
Пример #2 collection.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:param name="owner" select="'Nicolas Eliaszewicz'"/> <xsl:output method="html" encoding="iso-8859-1" indent="no"/> <xsl:template match="collection"> Hey! Welcome to <xsl:value-of select="$owner"/>'s sweet CD collection! <xsl:apply-templates/> </xsl:template> <xsl:template match="cd"> <h1><xsl:value-of select="title"/></h1> <h2>by <xsl:value-of select="artist"/> - <xsl:value-of select="year"/></h2> <hr /> </xsl:template> </xsl:stylesheet>
Коментарии
Here's a very simple example on how to use PHP5 to transform a XML file using a XSL file.
<?php
$xslDoc = new DOMDocument();
$xslDoc->load("collection.xsl");
$xmlDoc = new DOMDocument();
$xmlDoc->load("collection.xml");
$proc = new XSLTProcessor();
$proc->importStylesheet($xslDoc);
echo $proc->transformToXML($xmlDoc);
?>
For the sake of simplicity there's no error handling on this code. I hope this helps.
This is more convenient, no files nor verbose variables needed:
<?php
$xslt = new XSLTProcessor();
$xslt->importStylesheet(new SimpleXMLElement($xslt_string));
echo $xslt->transformToXml(new SimpleXMLElement($xml_string));
?>