Jump to content

PHP5 and XSLT


aspnetguy

Recommended Posts

I am using PHP5 and I need to transform an XML string with an XSL string into HTML output.I have this so far and really am not sure where to go from here or even if I am on the right track.

function displayAds($xml_str)	{		$xml = new DOMDocument();		$xml->loadXML($xml_str);				$xsl_str =	 '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">' .					'<xsl:output method="html" encoding="iso-8859-1" indent="no"/>' .					'<xsl:template match="links">' .					'<xsl:apply-templates/>' .					'</xsl:template>' .					'<xsl:template match="link">' .					'<a href="<xsl:value-of select="url"/>"><xsl:value-of select="text"/></a>' .					'<div><xsl:value-of select="description"/></div>' .					'</xsl:template>' .					'</xsl:stylesheet>';							$xsl = new DOMDocument();		$xsl->loadXML($xsl_str);		$xsl_proc = new XSLProcessor();		$xsl_proc->importStyleSheet($xsl);	}

the XML file looks like this

<?xml version="1.0"?><links>	<link>		<url/>		<text/>		<description/>	</link></links>

Link to comment
Share on other sites

God. It's times like this when I wonder when is W3Schools going to make those suggestions I sent to kaijim a long time ago. The tutorial is simply missing this. It's a good thing I wrote the XSLT FAQ to answer that exact question (number 1).

1. How to create attributes with XSLT?Unfortunately, that's the greatest loss of W3Schools' XSLT tutorial, so this question is not surprising at all. There are two ways you can create attributes in XSLT. Switch them depending on what you need:- Attribute Value Templates (often called AVT in short). It is an XPath expression, written directly into an attribute's value, surrounded by parenthesis. For example
<img src="{/foo/bar}"/>

Use it for simpler situations. AVT may also be used in the <xsl:attribute>, <xsl:element> and <xsl:processing-instruction> elements' name attribute.- The <xsl:attribute> element. The first non-XSLT parent takes that attribute. The equivalent of the above is

<img><xsl:attribute name="src"><xsl:value-of select="/foo/bar"/></xsl:attribute></img>

Use this element when you need to create the attribute only on certain conditions, or if you'd like to have the attribute name varying by putting an AVT inside it.

That's exactly your mistake too. You're using <xsl:value-of/> element inside an attribute's value, which makes the XSLT illformed XML document.Btw, here's a version with error handles (and the actual fixes):
function displayAds($xml_str)	{//Prepare the XML$xml = new DomDocument;if (!$xml->loadXML($xml_str)) {die('The XML is not well formed.');}		$xsl_str =	 '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">' .					'<xsl:output method="html" encoding="iso-8859-1" indent="no"/>' .					'<xsl:template match="links">' .					'<xsl:apply-templates/>' .					'</xsl:template>' .					'<xsl:template match="link">' .					'<a href="{url}"><xsl:value-of select="text"/></a>' .					'<div><xsl:value-of select="description"/></div>' .					'</xsl:template>' .					'</xsl:stylesheet>';//Prepare the XSLT$xsl = new DomDocument;if (!$xsl->loadXML($xsl_str)) {die('The XSLT is not well formed XML document.');}//Load the XSLT processor$xslt = new Xsltprocessor;$xslt->importStylesheet($xsl);//Execute the transformationif (!$transformation = $xslt->transformToXml($xml)) {die('Error during transformation. Possibly an endless loop or illegal XSLT construct.');}return $transformation;}

The next time you're in error, you'll know exactly what caused it.

Link to comment
Share on other sites

God. It's times like this when I wonder when is W3Schools going to make those suggestions I sent to kaijim a long time ago. The tutorial is simply missing this. It's a good thing I wrote the XSLT FAQ to answer that exact question (number 1).That's exactly your mistake too. You're using <xsl:value-of/> element inside an attribute's value, which makes the XSLT illformed XML document.Btw, here's a version with error handles (and the actual fixes):
function displayAds($xml_str)	 { //Prepare the XML $xml = new DomDocument; if (!$xml->loadXML($xml_str)) { die('The XML is not well formed.'); }		 $xsl_str =	 '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">' .					 '<xsl:output method="html" encoding="iso-8859-1" indent="no"/>' .					 '<xsl:template match="links">' .					 '<xsl:apply-templates/>' .					 '</xsl:template>' .					 '<xsl:template match="link">' .					 '<a href="{url}"><xsl:value-of select="text"/></a>' .					 '<div><xsl:value-of select="description"/></div>' .					 '</xsl:template>' .					 '</xsl:stylesheet>';  //Prepare the XSLT $xsl = new DomDocument; if (!$xsl->loadXML($xsl_str)) { die('The XSLT is not well formed XML document.'); }  //Load the XSLT processor $xslt = new Xsltprocessor; $xslt->importStylesheet($xsl);  //Execute the transformation if (!$transformation = $xslt->transformToXml($xml)) { die('Error during transformation. Possibly an endless loop or illegal XSLT construct.'); } return $transformation; }

The next time you're in error, you'll know exactly what caused it.

Thanks so much boen. I don't use XSL that much, let alone with PHP. You probably saved me a few hours of digging this evening.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...