ARTICLE AD BOX
I am creating web content that may be included from various locations. For images I need to set the @src such that the image is always found, no matter where the web page might be. I am setting a $relpath in php - in the main page - to allow for this. I am using lots of php in my generated content and it works fine, except when I try to set the src attribute for an image.
Here is a very simple test file:
<?xml version="1.0" encoding="UTF-8"?> <image href="images/spaceship.png"></image>I tried this transform:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="3.0"> <xsl:output method="html" encoding="UTF-8"/> <xsl:template match="/"> <xsl:apply-templates/> </xsl:template> <xsl:template match="image"> <img> <xsl:attribute name="src"> <xsl:processing-instruction name="php">echo $relpath</xsl:processing-instruction> <xsl:value-of select="@href"/> </xsl:attribute> </img> </xsl:template> </xsl:stylesheet>This results in the following php file:
<img src="echo $relpathimages/spaceship.png">Which obviously does not work. So I tried writing the php tags via xsl:text
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="3.0"> <xsl:output method="html" encoding="UTF-8"/> <xsl:template match="/"> <xsl:apply-templates/> </xsl:template> <xsl:template match="image"> <img> <xsl:attribute name="src"> <xsl:text disable-output-escaping="yes"><?php </xsl:text> <xsl:text>echo $relpath</xsl:text> <xsl:text disable-output-escaping="yes">?></xsl:text> <xsl:value-of select="@href"/> </xsl:attribute> </img> </xsl:template> </xsl:stylesheet>This makes the php start tag appear correctly, but disabling the output escaping on the end tag is completely ignored by the XSL processor. I know I do not need to use > for the closing bracket but the same result appears when replacing it with '>'.
<img src="<?php echo $relpath?>images/spaceship.png">The required output is this:
<img src="<?php echo $relpath?>images/spaceship.png">If there is another way of doing this let me know.
