Solved

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

Posted on 2012-04-05
5
1,330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 200 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:VieleFragen
ID: 37813024
Thanks; let me give that a try on my server.
0
 
LVL 2

Author Comment

by:VieleFragen
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

740 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