Jump to content

Php Includes With Xslt?


rlbewick
 Share

Recommended Posts

I am very new to the Transformation so please bare with me. I have a current site that uses a Javascript feed but, I would like the custom features of XML/XSLT to transform to HTML or XHTML.I use the code below to insert a navigation menu into all of my pages on the left side. With HTML this works well. Is there a function I can use in XSLT that will do the same thing?

<?php include $_SERVER['DOCUMENT_ROOT'].'phpincludes/nav_sidebar.php'; ?>

My head is sore from beating against the wall...Thanks everyone ;-)

Link to comment
Share on other sites

Do you just "echo" the sidebar, or do you integrate it into some kind of a sytem (e.g. a function you then call, a variable you place inside a container, etc.)? If you just "echo" it, there won't be a way to output it within XSLT.You can execute PHP functions from within XSLT, but they need to return a result, and THAT result is then inserted into the XSLT output. If your PHP function just "echo"es something, it will appear on screen before the XSLT is finished, and outputted.Natively speaking, XSLT has a "document()" function which lets you open up another XML document, which may also be XHTML code. You could then literally place that into the XSLT output. However, if you try to open up a PHP file, it won't work (in most cases), as the file will be opened up from source, not from its output.You could always insert the HTTP facing result directly, but that would considerably slow down your RSS page. Unfortunatly, if you don't want to make major arhitectural changes to the way you're currently working, this is your only choise. You can do it like so:

<xsl:copy-of select="document('http://example.com/phpincludes/nav_sidebar.php')/" />

(assuming the code of your navigation is well formed XHTML)

Link to comment
Share on other sites

Do you just "echo" the sidebar, or do you integrate it into some kind of a sytem (e.g. a function you then call, a variable you place inside a container, etc.)? If you just "echo" it, there won't be a way to output it within XSLT.You can execute PHP functions from within XSLT, but they need to return a result, and THAT result is then inserted into the XSLT output. If your PHP function just "echo"es something, it will appear on screen before the XSLT is finished, and outputted.Natively speaking, XSLT has a "document()" function which lets you open up another XML document, which may also be XHTML code. You could then literally place that into the XSLT output. However, if you try to open up a PHP file, it won't work (in most cases), as the file will be opened up from source, not from its output.You could always insert the HTTP facing result directly, but that would considerably slow down your RSS page. Unfortunatly, if you don't want to make major arhitectural changes to the way you're currently working, this is your only choise. You can do it like so:
<xsl:copy-of select="document('http://example.com/phpincludes/nav_sidebar.php')/" />

(assuming the code of your navigation is well formed XHTML)

Thank you for your input, boen robot.My php files are actually valid xhtml renamed to .php to use the php-include function so I can name them htm or html without any problem. I tried using..
<xsl:copy-of select="document('http://example.com/phpincludes/nav_sidebar.php')/" />

But, it did not produce any output with .php or .html.I am curious on how exactly to use the "document()" function. From what you have said this seems like a solution. Thanks again :-)

Link to comment
Share on other sites

Thank you for your input, boen robot.My php files are actually valid xhtml renamed to .php to use the php-include function so I can name them htm or html without any problem. I tried using..
<xsl:copy-of select="document('http://example.com/phpincludes/nav_sidebar.php')/" />

But, it did not produce any output with .php or .html.I am curious on how exactly to use the "document()" function. From what you have said this seems like a solution. Thanks again :-)

Did you replaced example.com with your real domain? Can you access the actual navigation by manually opening this in a browser? If it's a plain XHTML file, you may also try to access it without the http prefix, like so:
<xsl:copy-of select="document('nav_sidebar.php')/" />

(assuming nav_sidebar.php is in the same folder... adjust accordingly if not)The document() function works by parsing an XML file given as its argument. It then lets you select something from it. Adding only "/" selects the whole document. The xsl:copy-of element outputs the raw XML given to it, so the end result of this whole line is you outputting the contents of the XML file. Again, it works only if the file is a well-formed XML file (XHTML is XML, so if you have a valid XHTML, you also have XML).

Link to comment
Share on other sites

Did you replaced example.com with your real domain? Can you access the actual navigation by manually opening this in a browser? If it's a plain XHTML file, you may also try to access it without the http prefix, like so:
<xsl:copy-of select="document('nav_sidebar.php')/" />

(assuming nav_sidebar.php is in the same folder... adjust accordingly if not)The document() function works by parsing an XML file given as its argument. It then lets you select something from it. Adding only "/" selects the whole document. The xsl:copy-of element outputs the raw XML given to it, so the end result of this whole line is you outputting the contents of the XML file. Again, it works only if the file is a well-formed XML file (XHTML is XML, so if you have a valid XHTML, you also have XML).

Yes, I did replace "example.com" with my domain name but it still does not display any output. I will continue working with some different variations and let you know what works if anything. I would hope to help someone else with the same situation as mine given the chance.Got to work a couple of 12 hour night shifts so I may not be able to work on this for a couple of days, such is life.Thanks again for you help, I'll get back to you once I have tried some variables.
Link to comment
Share on other sites

Yes, I did replace "example.com" with my domain name but it still does not display any output. I will continue working with some different variations and let you know what works if anything. I would hope to help someone else with the same situation as mine given the chance.Got to work a couple of 12 hour night shifts so I may not be able to work on this for a couple of days, such is life.Thanks again for you help, I'll get back to you once I have tried some variables.
I have done some testing and have learned a few things. First of all the code would only work like this..
<xsl:copy-of select="document('nav_sidebar.php')" />

With the code with the additional "/" it would always fail validation and would not display in browser mode. Second of all I did not know that I could only use css and xslt in a limited form, that was the other part of my problem.Not bad for a guy who just stated xml 3 weeks ago part time. I must say I have learned much from DreamWeaver CS4, a bit from Style Vision and much more from this forum.I'll post a completed site when done, give me some time.Thanks again.. ;-)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...