0

I'm trying to use XSLT to generate some XHMTL with inline PHP. I've run across a problem with generating inline PHP in attributes.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:html="http://www.w3.org/1999/xhtml" version="1.0">
  <xsl:output method="xml"
       indent="yes"
       doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
       doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
       omit-xml-declaration="yes" />

  <xsl:template match="/">
    <html lang="<?php echo getLang(); ?>" xml:lang="<?php echo getLang(); ?>">
      <head>
 <xsl:processing-instruction name="php">include_title();</xsl:processing-instruction>

(Much more code ...)

gives the following results:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:html="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <?php include_title();?>

(Much more code ...)

Take note that the "lang" and "xml:lang" attributes of the html element are empty! So clearly, this is the wrong way to process inline PHP.

So does anyone know how to change the xsl code to get the desired result shown below?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:html="http://www.w3.org/1999/xhtml" lang="<?php echo getLang(); ?>" xml:lang="<?php echo getLang(); ?>">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <?php include_title();?>

    (Much more code ...)

Thanks, Kevin

3
  • Just to get the point: you want the return values from getLang() to be integrated into the transformation result during the transformation, while you want <?php include_title();?> to be included literally in the transformation output? Commented Nov 20, 2009 at 8:19
  • Or do you want to have the transformation output to include the PHP instruction <?php echo getLang(); ?> literally inside the attribute? Commented Nov 20, 2009 at 8:23
  • I would like the XSLT output to give the bottom PHP script with the <?php echo getLang(); ?> literally inside the attribute. This is so the dynamic nature of the webpage can be preserved. Commented Nov 20, 2009 at 18:52

4 Answers 4

2

&lt;?php ... &gt;

Sign up to request clarification or add additional context in comments.

1 Comment

This just gives the following result: lang="&lt;?php echo getLang(); ?&gt;" Sorry, try again.
1

maybe using the same directive you used for this:

<xsl:processing-instruction name="php">include_title();</xsl:processing-instruction>

?

Comments

0

Do you have the option of using XSLT 2.0? If you do, you can use character maps. Like this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:html="http://www.w3.org/1999/xhtml" version="2.0">
  <xsl:output method="xml"
       indent="yes"
       doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
       doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
       omit-xml-declaration="yes" 
       use-character-maps="php"/>

  <xsl:character-map name="php">
    <xsl:output-character character="«" string="&lt;"/>   
    <xsl:output-character character="»" string="&gt;"/>
  </xsl:character-map>

  <xsl:template match="/">
    <html lang="«?php echo getLang(); ?»" xml:lang="«?php echo getLang(); ?»">
      <head>
        <xsl:processing-instruction name="php">include_title();</xsl:processing-instruction>
      </head>
    </html>
  </xsl:template>
</xsl:stylesheet>

3 Comments

I like the way you think. This may be the solution, but I can't test it. I'm using the PHP5 XSL module, which uses libxslt version 1.1.22. As far as I can tell, it only supports XSLT1.0. :(
The XSLT 1.0 spec does not allow disabling output escaping for attribute values. I guess there is no way to get XSLT 1.0 to output unescaped angle brackets within attribute values. Perhaps could just add a "character map" style post processing stage after your XSLT transform?
After reading your suggestion yesterday that is exactly what I did using the str_replace() PHP function. I would prefer a less "messy" solution, but this works for the time being.
0

if you just echo getLang(), is it actually returning anything? Your code and environment description is very limited, so it is hard to tell what is getting processed as PHP, and what is getting processed by an XSLT processor.

What happens if you replace <?php echo getLang();?> with <?php echo '<?php echo getLang();?>' ;?>

2 Comments

Interesting idea. When I try this, I get the error message: "Unescaped '<' not allowed in attribute values."
If there is some more information you need please ask. I didn't include all the code and environment information because it seems to be superfluous.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.