htmlxsltxslt-2.0processing-instruction

Output php processing instruction inside attribute value


In my XSLT (2.0 - the output method is html) I have this:

<img>
    <xsl:attribute name="href">
        <xsl:text disable-output-escaping="yes">&lt;?php echo get_url(); ?&gt;</xsl:text>
    </xsl:attribute>
</img>

The output I want is as follows:

<img href="<?php echo get_url(); ?>">

The output I get is as follows:

<img href="<?php echo get_url(); ?&gt;">

Tried a bunch of different things to get the ">" coming out in the output instead of &gt; (CDATA marked sections etc.) but nothing seems to work. Strange that the less than sign works fine, but the greater than doesn't. I'm using Saxon-PE 9.5.1.7.


Solution

  • Use a character map with some characters you don't need elsewhere, here is an example (https://www.w3.org/TR/xslt20/#character-maps) adapted from the XSLT 2.0 spec:

    <img href="«?php echo get_url(); ?»"/>
    

    and

    <xsl:output method="html" use-character-maps="m1"/>
    
    <xsl:character-map name="m1">
        <xsl:output-character character="«" string="&lt;"/>
        <xsl:output-character character="»" string="&gt;"/>
    </xsl:character-map>
    

    Online example is at http://xsltransform.net/93dEHFP.

    As for disable-output-escaping, it does not work in attribute values as far as I know, that result that you get is not the result of disable-output-escaping but just the use of xsl:output method="html" (https://www.w3.org/TR/xslt-xquery-serialization/#HTML_ATTRIBS) mandating 'The HTML output method MUST NOT escape "<" characters occurring in attribute values.'.