Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I easily transform an XML document?

XML/XSL Related

How do I easily transform an XML document?

by  Karl Blessing  Posted    (Edited  )
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);

?>
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top