Regular PHP code inside XSLT template, is it possi

2019-03-02 10:46发布

问题:

I wonder If I can and how can I include code inside my XSLT template... I know I can use <xsl:choose> but that doesn't satisfy my needs, I want to add functions, variables etc...

<?xml version="1.0" encoding="utf-8"?>  
<xsl:stylesheet version="1.0"
xmlns:php="http://php.net/xsl"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:output method="html" encoding="UTF-8" indent="yes"/>  

<xsl:template match="BackgroundReportPackage">

<!--- here i would like to add code like ---->

if ($dateofcharge < 7) {
return '

<xsl:for-each select="Charge">
            <table class="special2" cellpadding="0">
                <tr class="tr-border-bottom">
                    <td class="front-td-text" valign="top">Charge ID: </td>
                    <td class="minimalec">
                    <xsl:value-of select="ChargeId"/>           
                    </td>
                </tr>

                <tr class="tr-border-bottom">
                    <td class="front-td-text" valign="top">Charge Type Classification: </td>
                    <td class="minimalec">
                    <xsl:value-of select="ChargeTypeClassification"/>
                    </td>
                </tr>                   

            </table>

';          
} else {
 do nothing
 }

 <!--- keep in mind that this code i've added is just for presentational purposes TO show you, how i want to use php code --->

</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Hope anyone can help!

回答1:

There is no real reason why you would write a template like that when XSLT can do if blocks. What you can look into is

  • XSLTProcessor::setParameter — Set value for a parameter

to change template values and

  • XSLTProcessor::registerPHPFunctions — Enables the ability to use PHP functions as XSLT functions

to use PHP functions inside the template. This will probably make more sense.



回答2:

No, you can't like that. You can include code from external namespaces, which is how you would write extension functions but I suspect you wouldn't be able to do them in PHP, they are normally in pre compiled languages as you have to load libraries to do it.

What is it you need to achieve. there is not that much that you can't achieve with XSL if you put your mind to it, certainly you wouldn't have a problem with something trivial like your example



标签: php xml xslt