Connecting Tech Pros Worldwide Help | Site Map

PHP code from XSL won't execute

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#1: Aug 12 '08
Hi,

I'm running into a problem with the <xsl:processing-instruction> element (Sablotron).

purpose:
I generate a xhtml fragment from a xml file via xslt. This is printed (echo) to the output (i.e. all xml/xsl processing is done on the server). This is fine, as long as the output contains only xhtml.

problem:
one special page uses AJAX, where the result is taken from a MySQL database. To make it easier writing copy, I (sucessfully) use php code to paste the link (and the content, if Javascript is not activated):
Expand|Select|Wrap|Line Numbers
  1. # top of included file
  2. # lib.db.php is used for MySQL processing
  3.     include_once(SYS_DIR . "lib.db.php");
  4.     $artikel =& new AjaxRequest;
  5.  
  6. # for each link
  7.     $artikel->getArticle("__DB-entry-ID__");
  8.  
this will result in:
Expand|Select|Wrap|Line Numbers
  1. # for each link
  2.   <div id="__DB-entry-ID__" class="einzug">
  3.     <a id="a__DB-entry-ID__" class="cfa" href="main.php?AOD=__DB-entry-ID__#a__DB-entry-ID__">Artikeltext anschauen</a>
  4.   </div>
  5.  
so far no problem when I include it from a file–but now the same as output from a xsl transformation:

It will readily transform the <xsl:processing-instruction> into php code, but the code is not parsed afterwards (even though the DOM recognizes it later as a processing instruction) it looks more like a CDATA section. I find the PHP code afterwards in the xhtml file, just like that:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     include_once(SYS_DIR . "lib.db.php");
  3.     $artikel =& new AjaxRequest;
  4. ?>
  5.   <h4><a class="hook" id="h6">__Title-of-Item__</a></h4>
  6.   <p>__Publication-Date__, __Newspaper-Name__</p>
  7.   <div class="mitte">
  8.     <img src="bilder/presse/__IMG-source__" title="__IMG-title__" width="__IMG-width__" height="__IMG-height__" />
  9.   </div>
  10. <?php
  11.     $artikel->getArticle("__DB-entry-ID__");
  12. ?>
  13.  
does anyone have an idea, how I can get PHP to parse the output from the xsl transformation?

thanks in advance

file listings:

xml file (presse.xml) (representative entry):
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE presse [ ... ]]>
  2. <presse
  3.     xmlns="http://www.kulturbeutel-leipzig.net/XML/presse"
  4.     xmlns:link="http://www.kulturbeutel-leipzig.net/XML/link"
  5.     xmlns:dcterms="http://purl.org/dc/terms/">
  6.  
  7. <collection title="__Title-of-Item__">
  8.   <item db="__DB-entry-ID__">
  9.     <zeitung>__Newspaper-Name__</zeitung>
  10.     <datum>__Publication-Date__</datum>
  11.     <thumbnail src="__IMG-source__" wdt="__IMG-width__" hgt="__IMG-height__">
  12.        <title>__IMG-Title__</title>
  13.     </thumbnail>
  14.   </item>
  15. </collection>
  16.  
  17. </presse>
  18.  
xsl file (sys.presse.xsl)
(I skip the further templates here, because they don't provide more information):
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="iso-8859-1" ?>
  2. <xsl:stylesheet version="1.0"
  3.     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  4.     xmlns:html="http://www.w3.org/1999/xhtml"
  5.     xmlns:presse="http://www.kulturbeutel-leipzig.net/XML/presse">
  6.  
  7. <xsl:output
  8.     method="xml"
  9.     encoding="iso-8859-1"
  10.     omit-xml-declaration="yes"
  11.     indent="yes" />
  12.  
  13. <xsl:template match="/">
  14.   <xsl:element name="div">
  15.     <xsl:attribute name="id">
  16.       <xsl:text>zeitung</xsl:text>
  17.     </xsl:attribute>
  18.  
  19.     <xsl:text>
  20.   </xsl:text>
  21.     <xsl:element name="h1">
  22.       <xsl:text>Zeitungsausschnitte</xsl:text>
  23.     </xsl:element>
  24.  
  25.     <xsl:text>
  26.   </xsl:text>
  27.     <xsl:element name="p">
  28.       <xsl:attribute name="class">
  29.         <xsl:text>mitte</xsl:text>
  30.       </xsl:attribute>
  31.       <xsl:text>oder was man so in den Zeitungen so über uns findet …</xsl:text>
  32.     </xsl:element>
  33.  
  34. # here comes the php code, if I don't escape & and > I'll get a parser error
  35.     <xsl:processing-instruction name="php">
  36.     include_once(SYS_DIR . "lib.db.php");
  37.     $artikel =&amp;amp; new AjaxRequest;
  38.     </xsl:processing-instruction>
  39.  
  40.     <xsl:apply-templates />
  41.  
  42.   </xsl:element>
  43. </xsl:template>
  44.  
  45. <xsl:template match="//presse:collection">
  46.   <xsl:call-template name="heading" />
  47.   <xsl:for-each select="child::presse:item">
  48.     <xsl:call-template name="titel" />
  49.     <xsl:if test="attribute::db">
  50.         <xsl:call-template name="text" />
  51.     </xsl:if>
  52.     <xsl:if test="child::presse:thumbnail">
  53.        <xsl:call-template name="bild" />
  54.     </xsl:if>
  55.     <xsl:if test="child::presse:file">
  56.        <xsl:call-template name="download" />
  57.     </xsl:if>
  58.     <xsl:if test="child::presse:url">
  59.        <xsl:call-template name="verweis" />
  60.     </xsl:if>
  61.   </xsl:for-each>
  62. </xsl:template>
  63. </xsl:stylesheet>
  64.  
php calling hierarchy:

top level script: main.php
creates an object and calls the output function
Expand|Select|Wrap|Line Numbers
  1.     require_once("system/lib.main.php");
  2.     $inhalt =& new ContentOutput($_GET["__page-ID__"]);
  3.     $inhalt->DataInhalt();
  4.  
php library: lib.main.php
does the part of putting it all together
Expand|Select|Wrap|Line Numbers
  1. # the xslt parser (in a nutshell) (SYS_DIR is where all the files are)
  2.  
  3.     function parseXSLT($xml, $xsl, $par = NULL, $args = array()) {
  4.         $xml = SYS_DIR . $xml;
  5.         $xsl = SYS_DIR . $xsl;
  6.         $handle = xslt_create();
  7.         $parsedfile = xslt_process($handle, $xml, $xsl, NULL, $args, $par);
  8.         xslt_free($handle);
  9.         return $parsedfile;
  10.     }
  11.  
  12. # $this->ID is set when calling the object in the first place
  13. # $this->Data is an array of objects containing the information for every web page like: include file or files and parameters for xslt (either of them)
  14.  
  15.     function DataInhalt() {
  16. # set of parameters for xsl processing
  17.         $parameter = NULL;
  18.         $xmlfile = 'Struktur.xml';
  19.         $xslfile = $this->Data[$this->ID]->datei["xsl"];
  20.         $tempxml = $this->Data[$this->ID]->datei["xml"];
  21.         $temppar = $this->Data[$this->ID]->datei["para"];
  22.         $tempfile = $this->Data[$this->ID]->datei["file"];
  23.         if (!empty($tempxml)) {
  24.             $xmlfile = $tempxml;
  25.         }
  26.         if (!empty($temppar)) {
  27.             $parameter = array($temppar => $this->ID);
  28.         }
  29. # if there's a file, include it an quit function
  30.         if (!empty($tempfile)) {
  31.             include(BASE_DIR . $tempfile);
  32.             return;    
  33.         }
  34. # do xsl processing
  35.         if (!$KBL_inhalt = $this->parseXSLT($xmlfile, $xslfile, $parameter)) {
  36.             echo '<p class="mitte b">Parser Error</p>';
  37.             return;
  38.         }
  39.         else {
  40. # print result
  41.              print $KBL_inhalt;
  42.         }
  43.     }
  44.  
System: Apache 1.3.4 / Mac OS X 10.4.11 / PHP 4.3.1 (from entropy.ch)
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#2: Sep 5 '08

re: PHP code from XSL won't execute


problem solved

I now write the result string to file (at the first page load after the xml was updated) and load that file via include().
Reply


Similar PHP bytes