×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

XML/XSL Related

How do I easily transform an XML document? by kb244
Posted: 15 Dec 04 (Edited 15 Dec 04)

I started looking into XML/XSL when one of my clients had no database support at all, but wanted a quick and easy way to hold data such as support group entries.

XML/XSL alone would work great, except I would have two problems that I know of.

1) XML/XSL on the client side is browser dependant most work ok, but not all , and the differences might appear.
2) I need the content of the transformed XML inside of another document, somewhat like a PHP include.

For understanding sakes here is a layout of an .xml document and it's corresponding .xsl document.

support.xml

CODE

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="support.xsl"?>

<supportgroups>
    <region name="Western" updated="12/07/04">
        <city name="Holland">
            <supportgroup>
                <title>Holland District Library</title>
                <date>Every 1st Thursday</date>
                <time>7:00PM - 8:30PM</time>
                <location>Address</location>
                <notes></notes>
                <facilitator>
                    <name>Facilitator One</name>
                    <phone>Phone One</phone>
                </facilitator>
            </supportgroup>
            <supportgroup>
                <title>Rest Haven Care Center</title>
                <date>Every 2nd Tuesday</date>
                <time>1:30PM</time>
                <location>Address</location>
                <notes></notes>
                <facilitator>
                    <name>Facilitator One</name>
                    <phone>Phone One</phone>
                </facilitator>
                <facilitator>
                    <name>Facilitator Two</name>
                    <phone>Phone Two</phone>
                </facilitator>
            </supportgroup>    
        </city>
        </region>
</supportgroups>

support.xsl

CODE

<?xml version="1.0" encoding="ISO_8859-1" ?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:template match="/">
            <xsl:apply-templates select="supportgroups" />
</xsl:template>

<xsl:template match="supportgroups">
    <html>
        <body>
        <xsl:apply-templates select="region" />
        </body>
     </html>
</xsl:template>

<xsl:template match="region">
<table border="0" width="95%" cellpadding="2" cellspacing="0">
    <tr>
        <td>                                    
            <div style="border: 1px solid #000000; padding: 0px; background: #FFFFFF; ">
                <table>
                    <tr>
                        <td class="bodysubhead">
                            <xsl:value-of select="./@name" /> Area Support Groups (List Updated <xsl:value-of select="./@updated" />)
                        </td>
                    </tr>
                </table>
                <div style="padding: 3px;">
                <p>
                    <xsl:apply-templates select="city" />
                </p>
                </div>
            </div>
        </td>
    </tr>
</table>
</xsl:template>

<xsl:template match="city">
<table style="border:0;cellpadding:2;width:500px;cellspacing:0;">
    <tr>
        <td>                                    
            <div style="border:1px solid #000000; padding:0px; background:#F7F6D6;">
                <table>
                    <tr>
                        <td class="bodysubhead">
                            <xsl:value-of select="./@name" />
                        </td>
                    </tr>
                </table>
                <p>
                    <ul>
                        <xsl:apply-templates select="supportgroup" />
                    </ul>
                </p>
            </div>
        </td>
    </tr>
</table>
</xsl:template>

<xsl:template match="supportgroup">
    <li><b>
        <xsl:value-of select="./title"/>
        </b><br/>
        <xsl:value-of select="./date"/> @ <xsl:value-of select="./time"/><br/>
        <xsl:value-of select="./location"/> -- <xsl:value-of select="../@name"/><br/>
        <xsl:if test="./notes !='">
            <i><b><xsl:value-of select="./notes"/></b></i><br/>
        </xsl:if>
        Facilitator :
        <xsl:apply-templates select="facilitator" />
    </li>
</xsl:template>

<xsl:template match="facilitator">
    <xsl:value-of select="./name" />, <xsl:value-of select="./phone" /><br />
</xsl:template>

</xsl:stylesheet>

and finally how to spit it out in PHP

CODE

<?
$xh = xslt_create(); //Creates an XSLT transform object
$myResult = xslt_process(
                         $xh,
                         'support.xml',
                         'support.xsl'
                        );
echo $myResult; //Sends the to the browser the transformed XML into HTML appearance.
xslt_free($xh); // Free's the object from memory.
?>


If the simple lines of code above does not work (ie: I just had problems getting it to run on my windows version of PHP with the php_xslt.dll extension loaded) Use this code sniplet for the PHP.

CODE

<?

// Xml and XSL files
$xml_file = "complex.xml";
$xsl_file = "complex.xsl";

// Allocate a new XSLT processor
$xh = xslt_create();
$fileBase = 'file://' . getcwd () . '/';
xslt_set_base ( $xh, $fileBase );

// Process the document
$result = xslt_process($xh, $xml_file, $xsl_file);
if (!$result) {
    // Something croaked. Show the error
    echo 'XSLT processing error: ' .xslt_error($xh) ;
}
else {
    // Output the resulting HTML
    echo $result;
}

// Destroy the XSLT processor
xslt_free($xh);

?>

Back to PHP FAQ Index
Back to PHP Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close