Solved

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

Posted on 2012-04-05
5
1,292 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:VieleFragen
  • 3
  • 2
5 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 200 total points
Comment Utility
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
Comment Utility
OK, I changed that bit,
threw the PHP, XML and XSLT on my server and it all works nicely together
0
 
LVL 1

Author Comment

by:VieleFragen
Comment Utility
Thanks; let me give that a try on my server.
0
 
LVL 1

Author Comment

by:VieleFragen
Comment Utility
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
Comment Utility
welcome
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to count occurrences of each item in an array.
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now