?
Solved

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

Posted on 2012-04-05
5
Medium Priority
?
1,510 Views
Last Modified: 2012-06-27
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)?
0
Comment
Question by:Alan Varga
  • 3
  • 2
5 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 800 total points
ID: 37811957
This line

    <xsl:template match="contact[@title=$title]">

is not allowed in XSLT1

since you are explicit in the apply templates, you can safely replace this with
    <xsl:template match="contact">
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 37811987
OK, I changed that bit,
threw the PHP, XML and XSLT on my server and it all works nicely together
0
 
LVL 2

Author Comment

by:Alan Varga
ID: 37813024
Thanks; let me give that a try on my server.
0
 
LVL 2

Author Comment

by:Alan Varga
ID: 37814358
That small change was exactly the problem; the page works like a charm now.  Thanks very much!
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 37814360
welcome
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month15 days, 19 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question