[xsl] XSLT w/ PHP, ASP, JSP Processing Instructions

Subject: [xsl] XSLT w/ PHP, ASP, JSP Processing Instructions
From: Michael B Allen <mba2000@xxxxxxxxxx>
Date: Mon, 5 Dec 2005 13:28:32 -0500
On Mon, 5 Dec 2005 10:15:24 GMT
David Carlisle <davidc@xxxxxxxxx> wrote:

> 
> If you need an html prodcessing instruction that ends with a ? then all
> you need to do is make the last character in your
> xsl:processing-instruction elementbe a ?

Actually I found a post somewhere that does just that and it works
with xsltproc:

<?xml version="1.0"?>

  <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output method="html"
      encoding="ISO-8859-1"
      doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"/>
       
  <xsl:template match="/|@*|node()">
      <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
  </xsl:template>
  
  <xsl:template match="processing-instruction()">
      <xsl:processing-instruction name="php">
          <xsl:value-of disable-output-escaping="yes" select="."/>
          <xsl:text>?</xsl:text>
      </xsl:processing-instruction>
  </xsl:template>

But I'm a little worried because Saxon complains that the two templates
are ambiguous. I guess node() also selects PIs? Is there a way to excplide
'php' PIs from The Identity Transform?

Let me exaplain the problem from the beginning. Perhaps someone can
recommend definitive technique. I don't understand this stuff well
enough yet.

The problem is that the HTML spec does not permit processing instructions
like:

  <?foo ... ?>

They must be in the form:

  <?foo ... >

with NO trailing '?'. And this is exhibited in XSTL processors emitting
html. Unfortunately this is not in touch with reality as there are
several embedded scripting languages that use PIs in the first form. These
include PHP, ASP, and JSP. An example PHP script might look like:

  <p/>Answer: <?php
      if ($value > 1.0) {
          echo "<p/>The value is greater than 1.";
      }
  ?>

In the user's browser, this should be rendered as:

  Answer: The value is greater than 1.

But the missing '?' causes the script (PHP at least) to fail. If we
use the xml output method to subvert the no trailing '?' requirement,
all PIs still must not contain '>' or '<'. Even if it were practical
to use entity references for these characters (which it's not), PHP
will generate a parse error on them. Also XHTML does not have <?xml
version="1.0"?> at the top AFAIK.

Can someone recommend a robust, compliant method for transforming XML
to HTML or XHTML with PHP compatible processing instructions? Surely
the XSLT founding fathers must have considered this use-case.

Thanks,
Mike

Current Thread