Regular PHP code inside XSLT template, is it possi

2019-03-02 10:29发布

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!

标签: php xml xslt
2条回答
乱世女痞
2楼-- · 2019-03-02 10:40

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

查看更多
等我变得足够好
3楼-- · 2019-03-02 10:42

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

to change template values and

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

查看更多
登录 后发表回答