[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2012-04-05
5
Medium Priority
?
1,456 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
[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 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

Independent Software Vendors: 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

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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…
Suggested Courses

650 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