Re: [xsl] embedded php tags in xml

Subject: Re: [xsl] embedded php tags in xml
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 05 Apr 2004 17:31:31 -0400
At 2004-04-05 15:07 -0600, Jeffrey Moss wrote:
I have a source document I am running through a stylesheet that has this in
it:

<?xml version="1.0" encoding="ISO-8859-1"?>

<nbn:template xmlns:nbn="nbn:namespace"
xmlns="http://www.w3.org/1999/xhtml";>
<DIV CLASS="content">
 <?php
   print 'Content ('.__FILE__.')';
 ?>

This is the default action content template.

</DIV>
</nbn:template>

I have some default templates set up to handle items in the xhtml and nbn
namespaces,

Fine ...


normally anything without a namespace is part of the xhtml
namespace by default in my xsl stylesheet,

Okay ...


and all nodes in this namespace match a xhtml:* template.

Fine ...


When I run it through xsltproc with verbose debugging, it spits this out:
xsltProcessOneNode: no template found for php
xsltProcessOneNode: no template found for text
xsltDefaultProcessOneNode: copy text

Right ... because the built-in template rule for a processing instruction is to add nothing to the result tree and the built-in template rule for text is to add its value to the result tree.


Now it looks like everything with a ? falls into a different namespace?

I don't know your use of terminology here ... a processing instruction is not an element and does not live in any namespace.


Or is that some special instruction?

Yes, it is called a processing instruction.


Ideally I'd like this to just copy right over, as if it were an ordinary
XHTML tag.

An example is below ... it copies the processing instruction instead of letting the built-in template rules handle it ... but it *isn't* an ordinary XHTML tag ... it isn't an element.


Note that the XPath data model does not preserve whitespace between the PI target and the first non-whitespace character of its content.

Also, I was curious about embedding php code in my xsl stylesheets, I
suppose I need to use [CDATA[ ,

Use it for what?


if anybody is familiar with this I would appreciate any input.

The PHP pages on my web site are created from XSLT complete with all the background PHP logic triggered by the presence of elements.


I hope this helps.

......................... Ken

T:\ftemp>type jeffrey.xml
<?xml version="1.0" encoding="ISO-8859-1"?>

<nbn:template xmlns:nbn="nbn:namespace"
xmlns="http://www.w3.org/1999/xhtml";>
<DIV CLASS="content">
 <?php
   print 'Content ('.__FILE__.')';
 ?>

This is the default action content template.

</DIV>
</nbn:template>


T:\ftemp>type jeffrey.xsl <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:nbn="nbn:namespace" xmlns:xhtml="http://www.w3.org/1999/xhtml"; exclude-result-prefixes="nbn #default" version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="nbn:template">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="xhtml:*">
  <xsl:element name="{local-name()}">
    <xsl:apply-templates select="node()"/>
  </xsl:element>
</xsl:template>

<xsl:template match="processing-instruction('php')">
  <xsl:copy/>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>saxon jeffrey.xml jeffrey.xsl
<?xml version="1.0" encoding="utf-8"?>

<DIV>
 <?php print 'Content ('.__FILE__.')';
 ?>

This is the default action content template.

</DIV>

T:\ftemp>

--
Public courses: Spring 2004 world tour of hands-on XSL instruction
Each week:   Monday-Wednesday: XSLT/XPath; Thursday-Friday: XSL-FO
Hong Kong May 17-21; Bremen Germany May 24-28; Helsinki June 14-18

World-wide on-site corporate, govt. & user group XML/XSL training.
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Breast Cancer Awareness  http://www.CraneSoftwrights.com/s/bc

Current Thread