0

Is there any way how to add php code to xml attribute using xslt?

XSLT

<xsl:variable name="php"><![CDATA[<?php echo htmlentities($_SERVER['PHP_SELF']);?>]]></xsl:variable>

<xsl:template match="/">
  <a href="{$php}" title="selfPage>
    <img src="nameOfPic" alt="..." />
  </a>
</xsl:template>

expected PHP output:

<a href="<?php echo htmlentities($_SERVER['PHP_SELF']);?>">
  <img src="nameOfPic" alt="..." />
</a>

This code below is working, but i don't know how to use in attribute this...

<xsl:value-of disable-output-escaping="yes" select="$php" />

EDIT: Yeah... sorry, the input HTML (in older version of answer) is an expected output, it's my fault.

3
  • 3
    It is not very clear what you want as output. Do you want to emit the PHP code as part of the output, or what? Could you add an example of some input and the output you expect. Commented Apr 17, 2014 at 17:51
  • Still not clear. Is that "output" supposed to be run through PHP still? I guess so, right? Commented Apr 18, 2014 at 9:46
  • Yeah, that output will be run as a PHP... I don't know, how to push php code into attribute... Commented Apr 18, 2014 at 10:04

2 Answers 2

1

I am not sure exactly what you are asking, but perhaps it is the xsl:attribute command that you are looking for here

<a title="selfPage">
   <xsl:attribute name="href">
       <xsl:value-of disable-output-escaping="yes" select="$php" />
   </xsl:attribute>
   <img src="nameOfPic" alt="..." />
</a>

You probably also want to make sure you set the output to "html" too, as what you are possibly trying to output won't be well-formed XML...

<xsl:output method="html" />

EDIT: If you really want to output without escaped value, then I think your only option could be to output as text, and that means writing something like this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text" indent="yes"/>

    <xsl:variable name="php"><![CDATA[<?php echo htmlentities($_SERVER['PHP_SELF']);?>]]></xsl:variable>

    <xsl:template match="/">
    &lt;a title="selfPage" href="<xsl:value-of select="$php" />" &gt;
       &lt;img src="nameOfPic" alt="..." /&gt;
    &lt;/a&gt;
    </xsl:template>
</xsl:stylesheet>

EDIT 2 - To avoid escaping all the tag names, another approach is to wrap much of the output in CDATA tags.

Try this XSLT too, which is slightly more readable.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" indent="yes" />

    <xsl:variable name="php"><![CDATA[<?php echo htmlentities($_SERVER['PHP_SELF']);?>]]></xsl:variable>

    <xsl:template match="/">
    <![CDATA[
       <a title="selfPage" href="]]><xsl:value-of disable-output-escaping="yes" select="$php" /><![CDATA[
          <img src="nameOfPic" alt="..." />
       </a>
       ]]>
    </xsl:template>
</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, but your code return this result, which is generated by saxon: <a title="selfPage" href="<?php echo htmlentities($_SERVER['PHP_SELF']);?&gt;"> <img src="nameOfPic" alt="..." /> </a> Main problem is a escaping character ">" to &gt in attribute href.
Hmm... this is working, but my output has to be a html5. Is there other way fot it? My current solution is very simple - instead of htmlentities($_SERVER['PHP_SELF']); i have only index.php, but it's not clear.
I am a bit unclear, to be honest. Are you outputting PHP or HTML5 here? If you are outputting HTML then the text inside the attributes will need to be escaped. If you are outputting PHP, then that it is the responsibility of the PHP to output HTML5, not the XSLT.
<xsl:output method="html" version="5.0" indent="yes" /> Simple question: How to generate that output below (HTML5 mixed with PHP in attribute)? <a href="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" /> Problem: php code in href attribute between quotes. Why do you not understand?
Please see the edit I made on the 18th to show how you can output that exactly. The main thing to note is you are outputting "text", not "html" here.
|
0

You could just pre-process your XSL templates (in other words: convert them into PHP files themselves), so the variables will already have been transformed into text:

<xsl:variable name="php">/path/to/script.php</xsl:variable>

<xsl:template match="/">
  <a href="{$php}" title="selfPage">
    <img src="nameOfPic" alt="..." />
  </a>
</xsl:template>

Business as usual from here on out.

Comments

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.