Link to home
Start Free TrialLog in
Avatar of Alan Varga
Alan VargaFlag for United States of America

asked on

PHP XSL transformation doesn't work when matching on an attribute

I am having trouble transforming XSL using PHP.  The multiple responses from the PHP server are:

Warning: XSLTProcessor::importStylesheet() [xsltprocessor.importstylesheet]: Undefined variable in /hermes/bosweb/web169/b1692/ipg.theburgenlandbunchor/Links/beta/example6/email.php on line 42

Warning: XSLTProcessor::importStylesheet() [xsltprocessor.importstylesheet]: compilation error: file /hermes/bosweb/web169/b1692/ipg.theburgenlandbunchor/Links/beta/example6/email.xsl line 54 element template in /hermes/bosweb/web169/b1692/ipg.theburgenlandbunchor/Links/beta/example6/email.php on line 42

Warning: XSLTProcessor::importStylesheet() [xsltprocessor.importstylesheet]: Failed to compile predicate in /hermes/bosweb/web169/b1692/ipg.theburgenlandbunchor/Links/beta/example6/email.php on line 42

Warning: XSLTProcessor::transformToXml() [xsltprocessor.transformtoxml]: No stylesheet associated to this object in /hermes/bosweb/web169/b1692/ipg.theburgenlandbunchor/Links/beta/example6/email.php on line 47

Warning: XSLTProcessor::transformToXml() [xsltprocessor.transformtoxml]: No stylesheet associated to this object in /hermes/bosweb/web169/b1692/ipg.theburgenlandbunchor/Links/beta/example6/email.php on line 48

Fatal error: XSL transformation failed. in /hermes/bosweb/web169/b1692/ipg.theburgenlandbunchor/Links/beta/example6/email.php on line 51

Open in new window


Here is my HTML/PHP file (PHP won't execute unless the file has a PHP suffix):
<!DOCTYPE html>

<html lang="en">

<head>
    <title>PHP example 6-Email Contacts</title>
    <meta charset="utf-8"/>
    <meta name="author" content="Alan Varga"/>
    <meta name="description" content="Embed PHP retrieval in HTML"/>
    <meta http-equiv="last-modified" content="Thu, 05 Apr 2012 07:30 CDT"/>
</head>

<body>

    <p>
No clicking necessary, PHP directly in HTML page.<br/>
Example of PHP transformation with parameters thanks to
    <a href="http://www.tonymarston.net/php-mysql/xsl.html">Tony Marston</a>.<br/><br/>
The Hello line demonstrates that PHP is at least working.
    </p>

    <p>

    <?php
        echo("Hello Alan");
    ?>
    <br/><br/>

The links editor is
    <?php
        $xml_doc = new DomDocument;
        $xml_doc->load("contacts.xml");

        $xsl_doc = new DomDocument;
        $xsl_doc->load("email.xsl");

        $xp1 = new XsltProcessor();
        $xp1->importStyleSheet($xsl_doc);

        $params['title'] = "links_editor";
        $xp1->setParameter("", $params);

        $html = $xp1->transformToXML($xml_doc);
        if ($html = $xp1->transformToXML($xml_doc)) {
            echo $html;
        } else {
            trigger_error("XSL transformation failed.", E_USER_ERROR);
        } // if
    ?>
    </p>

</body>
</html>

Open in new window


Here is the XML data file:
<?xml version="1.0" encoding="utf-8" ?>



<!--
===========================================
stylesheets used to format this datafile:
===========================================
<?xml-stylesheet type="text/xsl"
  href="email.xsl" ?>
-->



<contact_list>

  <metadata>
    <revdate>04/03/2012</revdate>
  </metadata>



  <contacts>
    <contact title="president">
      <name>Tom Steichen</name>
      <email>steichen@rr.triad.com</email>
    </contact>
    <contact title="links_editor">
      <name>Alan Varga</name>
      <email>aevarga@mc.net</email>
    </contact>
  </contacts>



</contact_list>

Open in new window


Here is the XSL stylesheet:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml"/>



<!--
===========================================
This stylesheet is used to format datafile:
contacts.xml
===========================================
<xsl:stylesheet  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
-->



<!--
=================================
parameters and global variables
=================================
testing only
<xsl:param name="title" select="'links_editor'"/>

live
<xsl:param name="title"/>

-->

<xsl:param name="title"/>



<!--
=================================
templates
=================================
-->
<xsl:template match="/">
  <xsl:apply-templates select="contact_list/contacts"/>
</xsl:template>



<xsl:template match="contact_list/contacts">
  <xsl:apply-templates select="contact[@title=$title]"/>
</xsl:template>



<xsl:template match="contact[@title=$title]">
  <xsl:variable name="elink">
    <xsl:text>mailto:</xsl:text>
    <xsl:value-of select="email"/>
  </xsl:variable>

  <a href="{$elink}">
      <xsl:value-of select="name" />
  </a>
</xsl:template>

</xsl:stylesheet>

Open in new window


To prove out that the XML and XSL files are properly formed and that the transformation works properly, I opened the XML file directly after uncommenting the stylesheet name in the XML file and adding the "select" attribute to the "title" parameter in the XSL file.  This successfully returned a hyperlink with my email address.

Is this a bug in PHP or the XSL translator, is it a setting that I can ask my PHP administrator to change, or is there an alternate way to code this (perhaps changing the "title" attribute to a child element)?
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK, I changed that bit,
threw the PHP, XML and XSLT on my server and it all works nicely together
Avatar of Alan Varga

ASKER

Thanks; let me give that a try on my server.
That small change was exactly the problem; the page works like a charm now.  Thanks very much!