Простой пример использования механизма трансформаций xslt:
Файл sample.xml
<?xml version="1.0"?>
<customers>
<customer>
<name>John Smith</name>
<address>123 Oak St.</address>
<state>WA</state>
<phone>(206) 123-4567</phone>
</customer>
<customer>
<name>Zack Zwyker</name>
<address>368 Elm St.</address>
<state>WA</state>
<phone>(206) 423-4537</phone>
</customer>
<customer>
<name>Albert Aikens</name>
<address>368 Elm St.</address>
<state>WA</state>
<phone>(206) 423-4537</phone>
</customer>
<customer>
<name>Albert Gandy</name>
<address>6984 4th St.</address>
<state>WA</state>
<phone>(206) 433-4547</phone>
</customer>
</customers>
Файл sample.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<HTML>
<BODY>
<TABLE border="1" cellspacing="0" cellpadding="2">
<xsl:apply-templates select="customers/customer">
<xsl:sort select="state"/>
<xsl:sort select="name"/>
</xsl:apply-templates>
</TABLE>
</BODY>
</HTML>
</xsl:template>
<xsl:template match="customer">
<TR>
<xsl:apply-templates select="name" />
<xsl:apply-templates select="address" />
<xsl:apply-templates select="state" />
<xsl:apply-templates select="phone" />
</TR>
</xsl:template>
<xsl:template match="name">
<TD STYLE="font-size:14pt font-family:serif">
<xsl:apply-templates />
</TD>
</xsl:template>
<xsl:template match="address">
<TD> <xsl:apply-templates /> </TD>
</xsl:template>
<xsl:template match="state">
<TD> <xsl:apply-templates /> </TD>
</xsl:template>
<xsl:template match="phone">
<TD> <xsl:apply-templates /> </TD>
</xsl:template>
</xsl:stylesheet>
Файл sample.php
<?php
$xslt = new xsltProcessor;
$xslt->importStyleSheet(DomDocument::load('sample.xsl'));
print $xslt->transformToXML(DomDocument::load('sample.xml'));
?>
Источник:
- 1 - user.su