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):
-
# top of included file
-
# lib.db.php is used for MySQL processing
-
include_once(SYS_DIR . "lib.db.php");
-
$artikel =& new AjaxRequest;
-
-
# for each link
-
$artikel->getArticle("__DB-entry-ID__");
-
this will result in:
-
# for each link
-
<div id="__DB-entry-ID__" class="einzug">
-
<a id="a__DB-entry-ID__" class="cfa" href="main.php?AOD=__DB-entry-ID__#a__DB-entry-ID__">Artikeltext anschauen</a>
-
</div>
-
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:
-
<?php
-
include_once(SYS_DIR . "lib.db.php");
-
$artikel =& new AjaxRequest;
-
?>
-
<h4><a class="hook" id="h6">__Title-of-Item__</a></h4>
-
<p>__Publication-Date__, __Newspaper-Name__</p>
-
<div class="mitte">
-
<img src="bilder/presse/__IMG-source__" title="__IMG-title__" width="__IMG-width__" height="__IMG-height__" />
-
</div>
-
<?php
-
$artikel->getArticle("__DB-entry-ID__");
-
?>
-
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):
-
<!DOCTYPE presse [ ... ]]>
-
<presse
-
xmlns="http://www.kulturbeutel-leipzig.net/XML/presse"
-
xmlns:link="http://www.kulturbeutel-leipzig.net/XML/link"
-
xmlns:dcterms="http://purl.org/dc/terms/">
-
-
<collection title="__Title-of-Item__">
-
<item db="__DB-entry-ID__">
-
<zeitung>__Newspaper-Name__</zeitung>
-
<datum>__Publication-Date__</datum>
-
<thumbnail src="__IMG-source__" wdt="__IMG-width__" hgt="__IMG-height__">
-
<title>__IMG-Title__</title>
-
</thumbnail>
-
</item>
-
</collection>
-
-
</presse>
-
xsl file (sys.presse.xsl)
(I skip the further templates here, because they don't provide more information):
-
<?xml version="1.0" encoding="iso-8859-1" ?>
-
<xsl:stylesheet version="1.0"
-
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
-
xmlns:html="http://www.w3.org/1999/xhtml"
-
xmlns:presse="http://www.kulturbeutel-leipzig.net/XML/presse">
-
-
<xsl:output
-
method="xml"
-
encoding="iso-8859-1"
-
omit-xml-declaration="yes"
-
indent="yes" />
-
-
<xsl:template match="/">
-
<xsl:element name="div">
-
<xsl:attribute name="id">
-
<xsl:text>zeitung</xsl:text>
-
</xsl:attribute>
-
-
<xsl:text>
-
</xsl:text>
-
<xsl:element name="h1">
-
<xsl:text>Zeitungsausschnitte</xsl:text>
-
</xsl:element>
-
-
<xsl:text>
-
</xsl:text>
-
<xsl:element name="p">
-
<xsl:attribute name="class">
-
<xsl:text>mitte</xsl:text>
-
</xsl:attribute>
-
<xsl:text>oder was man so in den Zeitungen so über uns findet …</xsl:text>
-
</xsl:element>
-
-
# here comes the php code, if I don't escape & and > I'll get a parser error
-
<xsl:processing-instruction name="php">
-
include_once(SYS_DIR . "lib.db.php");
-
$artikel =&amp; new AjaxRequest;
-
</xsl:processing-instruction>
-
-
<xsl:apply-templates />
-
-
</xsl:element>
-
</xsl:template>
-
-
<xsl:template match="//presse:collection">
-
<xsl:call-template name="heading" />
-
<xsl:for-each select="child::presse:item">
-
<xsl:call-template name="titel" />
-
<xsl:if test="attribute::db">
-
<xsl:call-template name="text" />
-
</xsl:if>
-
<xsl:if test="child::presse:thumbnail">
-
<xsl:call-template name="bild" />
-
</xsl:if>
-
<xsl:if test="child::presse:file">
-
<xsl:call-template name="download" />
-
</xsl:if>
-
<xsl:if test="child::presse:url">
-
<xsl:call-template name="verweis" />
-
</xsl:if>
-
</xsl:for-each>
-
</xsl:template>
-
</xsl:stylesheet>
-
php calling hierarchy:
top level script: main.php
creates an object and calls the output function
-
require_once("system/lib.main.php");
-
$inhalt =& new ContentOutput($_GET["__page-ID__"]);
-
$inhalt->DataInhalt();
-
php library: lib.main.php
does the part of putting it all together
-
# the xslt parser (in a nutshell) (SYS_DIR is where all the files are)
-
-
function parseXSLT($xml, $xsl, $par = NULL, $args = array()) {
-
$xml = SYS_DIR . $xml;
-
$xsl = SYS_DIR . $xsl;
-
$handle = xslt_create();
-
$parsedfile = xslt_process($handle, $xml, $xsl, NULL, $args, $par);
-
xslt_free($handle);
-
return $parsedfile;
-
}
-
-
# $this->ID is set when calling the object in the first place
-
# $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)
-
-
function DataInhalt() {
-
# set of parameters for xsl processing
-
$parameter = NULL;
-
$xmlfile = 'Struktur.xml';
-
$xslfile = $this->Data[$this->ID]->datei["xsl"];
-
$tempxml = $this->Data[$this->ID]->datei["xml"];
-
$temppar = $this->Data[$this->ID]->datei["para"];
-
$tempfile = $this->Data[$this->ID]->datei["file"];
-
if (!empty($tempxml)) {
-
$xmlfile = $tempxml;
-
}
-
if (!empty($temppar)) {
-
$parameter = array($temppar => $this->ID);
-
}
-
# if there's a file, include it an quit function
-
if (!empty($tempfile)) {
-
include(BASE_DIR . $tempfile);
-
return;
-
}
-
# do xsl processing
-
if (!$KBL_inhalt = $this->parseXSLT($xmlfile, $xslfile, $parameter)) {
-
echo '<p class="mitte b">Parser Error</p>';
-
return;
-
}
-
else {
-
# print result
-
print $KBL_inhalt;
-
}
-
}
-
System: Apache 1.3.4 / Mac OS X 10.4.11 / PHP 4.3.1 (from entropy.ch)