1

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!

3
  • You can certainly have a PHP script generate xslt as output. But I don't think you can have an xslt template call on PHP to evaluate portions of the template. Commented Oct 18, 2011 at 16:12
  • php.net/manual/en/xsltprocessor.registerphpfunctions.php and see also php.net/manual/en/function.stream-wrapper-register.php - you can access streams from within XML/XSLT as well, like myvar://variablename (if you have build one that does this). Commented Oct 18, 2011 at 16:12
  • You did not accept an answer yet. Can you please clarify what you are looking for in an answer and why the given answers do not satisfy you. Commented Dec 24, 2011 at 9:55

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

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.