Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-29-2008, 01:28 AM   PM User | #1
rizzo89
New Coder

 
Join Date: Nov 2008
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
rizzo89 is an unknown quantity at this point
XML XSLT and PHP AddType Question

Hello,
I am trying to come up with a system to style sitemaps. I have everything working except I can't execute <?php> tags inside the .xslt document.
I think that I am not enabling something in the .htaccess but, I just can't figure it out. Help would be very awesome.

MODULES
I have this extension loaded.... do I need others?
php_xsl.dll


Here are my various pages.

.XML
Code:
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="application/xslt+xml" href="/stylesheets/sitemap.xsl" ?>

<urlset>
<url>
<loc>
</loc>
<lastmod>
</lastmod>
</url>
</urlset>
.XSL
Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">

<xsl:output method="html"/>
<xsl:template match="/">

<html>
<?php $thisPage="sitemap"; ?>

<head>
<title><?php echo $thisPage ?></title>
<?php include"./includes/meta.php"?>
<?php include"./includes/link.php"?>
</head>

<body>
<div id="wrapper">

<div id="tank" >
<h1><?php echo $thisPage ?></h1>
<h2></h2>
	<?php include"./includes/header.php" ?>

	<div id="nav">
	
	<?php include"./includes/nav.php" ?>
	</div><!--End of nav-->

<div id="leftcolumn">

<h1>sitemap</h1>
    <table border="1">
    <tr>
      <th align="left">URLs</th>
      <th align="left">Last Modified</th>
    </tr>
    <xsl:for-each select="urlset/url">
    <tr>
      <td><xsl:value-of select="loc"/></td>
      <td><xsl:value-of select="lastmod"/></td>
    </tr>
    </xsl:for-each>
    </table>


</div> <!--End of Left Column-->

<div id="rightcolumn">
</div> <!--End of Right Column-->

<div class="clear"></div>

<div id="footer" style="margin-top: 10px;">
<?php include"./includes/footer.php" ?>	
</div><!--End of Footer-->
</div><!--End of tank-->
	</div><!--End of Wrapper-->
</body>
</html>
</xsl:template>
</xsl:stylesheet>
.HTACCESS (I have tried several combinations of the types below)
Code:
AddType x-mapp-php5 .html .htm .xsl .xml
AddType application/x-httpd-php .xml .xsl .html
AddType	text/xsl .xslt .xsl
AddType	application/xml	.xml
AddType	application/xhtml+xml .xhtml
rizzo89 is offline   Reply With Quote
Old 11-29-2008, 04:44 PM   PM User | #2
byuhobbes85
Regular Coder

 
byuhobbes85's Avatar
 
Join Date: Oct 2006
Location: Ames, Iowa, USA
Posts: 116
Thanks: 9
Thanked 4 Times in 4 Posts
byuhobbes85 is an unknown quantity at this point
Question

I have had a similar problem. I have an XML file that is successfully transformed using an XSL stylesheet, but when I copy that XML into a PHP file (and echo out the tags starting with <? ) it does not transform the XML.

I'm running IIS, so I don't know if .htaccess will work for me.

I have included examples below. The XSL stylesheet doesn't do much, I was just trying to do a case-in-point.

Here is the first bit of the plain XML.
Code:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="ScoTest.php"?>

<TestItems>
<item>
	<itemId>0-48de9c82a963f</itemId>
	<itemType>scriptMasculineFeminine</itemType>
	<instructions>Identify whether the following word is masculine or feminine.</instructions>
	<stimulus>
		<stimulusType>script</stimulusType>
		<stimulusLanguage>arabic</stimulusLanguage>
		<stimulusValue> أنتِ </stimulusValue>
	</stimulus>
	<responses>
		<responseType>text/script</responseType>
		<responseLanguage>English</responseLanguage>
		<response>
			<correct>false</correct>
			<responseValue>masculine</responseValue>
		</response>
		<response>
			<correct>true</correct>
			<responseValue>feminine</responseValue>
		</response>
	</responses>
</item>
...
...
Here is the first bit of the PHP.
PHP Code:
<?php
echo '<?xml version="1.0"?>\n';
echo 
'<?xml-stylesheet type="text/xsl" href="ScoTest.xsl"?>';
?>

<TestItems>
<item>
    <itemId>0-48de9c82a963f</itemId>
    <itemType>scriptMasculineFeminine</itemType>
    <instructions>Identify whether the following word is masculine or feminine.</instructions>
    <stimulus>
        <stimulusType>script</stimulusType>
        <stimulusLanguage>arabic</stimulusLanguage>
        <stimulusValue> أنتِ </stimulusValue>
    </stimulus>
    <responses>
        <responseType>text/script</responseType>
        <responseLanguage>English</responseLanguage>
        <response>
            <correct>false</correct>
            <responseValue>masculine</responseValue>
        </response>
        <response>
            <correct>true</correct>
            <responseValue>feminine</responseValue>
        </response>
    </responses>
</item>
Here is the stylesheet.
Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="html"
		encoding="utf-8"
		omit-xml-declaration="yes" />
	<xsl:template match="/">
			<p class="header1">SCO Test</p>
			<p>
			<xsl:apply-templates />
			</p>
	</xsl:template>
	<xsl:template match="item"><span class="tooltip"><xsl:value-of select="itemId" /><span class="tooltip_text"><xsl:value-of select="instructions" /></span></span><br /></xsl:template>
</xsl:stylesheet>
Like I said, the transformation works with the plain XML file, but not with the PHP file.
__________________
-- </byuhobbes>
byuhobbes85 is offline   Reply With Quote
Old 11-29-2008, 05:09 PM   PM User | #3
byuhobbes85
Regular Coder

 
byuhobbes85's Avatar
 
Join Date: Oct 2006
Location: Ames, Iowa, USA
Posts: 116
Thanks: 9
Thanked 4 Times in 4 Posts
byuhobbes85 is an unknown quantity at this point
I think I have a solution.

If you want to execute PHP in your XML or XSL documents, take the following steps.
  • place the XML/XSL in a file with a PHP extension
  • place any XML tags that begin with <? into PHP echo statements so they are not mistaken by the PHP processor as PHP code
  • designate the mime-type using the PHP command below

Here is how you designate the mime-type
PHP Code:
header("content-type: application/xhtml+xml; charset=utf-8"); 
Hope this helps.
__________________
-- </byuhobbes>
byuhobbes85 is offline   Reply With Quote
Old 11-30-2008, 09:05 PM   PM User | #4
rizzo89
New Coder

 
Join Date: Nov 2008
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
rizzo89 is an unknown quantity at this point
Thanks for the response. I still couldn't get it working though. I think I have given up on executing PHP within an xml/xsl document. My focus now is learning how to pass the xml and xsl files through a PHP file, then echo out the result...

Looks like I have some reading to do.

Article on using PHP with XML and XSLT
http://www.codewalkers.com/c/a/Misce...-XSLT-and-PHP/

XSLT @ PHP.net
http://us.php.net/manual/en/book.xslt.php

xslt_process @ PHP.net
http://us3.php.net/xslt_process

Download Sablotron XML toolkit
http://www.gingerall.org/sablotron.html

Download Expat XML Parser
http://expat.sourceforge.net/

Article on using expat with PHP
http://www.phpbuilder.com/columns/justin20000428.php3
rizzo89 is offline   Reply With Quote
Old 11-30-2008, 11:05 PM   PM User | #5
rizzo89
New Coder

 
Join Date: Nov 2008
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
rizzo89 is an unknown quantity at this point
I have found a solution for my application.

Now I just need an auto-sitemap generator so that it is updated for search engines and my site without me doing anything....??

sitemap.php
Code:
<html>....
<?php

   $xslDoc = new DOMDocument();
   $xslDoc->load("stylesheets/sitemap.xsl");

   $xmlDoc = new DOMDocument();
   $xmlDoc->load("sitemap.xml");

   $proc = new XSLTProcessor();
   $proc->importStylesheet($xslDoc);
   echo $proc->transformToXML($xmlDoc);
?>
</html>
sitemap.xsl
Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns="http://www.w3.org/1999/xhtml">
<xsl:template match="/">

    <table class="sitemap">
    <tr>
      <th align="left">URLs</th>
      <th align="left">Last Modified</th>
    </tr>
    
	<xsl:for-each select="urlset/url">
    <tr>
	<td>	
	<xsl:element name="a">
	<xsl:attribute name="href">
	<xsl:value-of select="loc"/>
	</xsl:attribute>
	<xsl:value-of select="loc"/>
	        </xsl:element>
	</td>
	<td><xsl:value-of select="lastmod"/></td>
    </tr>
    
	</xsl:for-each>
    </table>

</xsl:template>
</xsl:stylesheet>
sitemap.xml
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<urlset>
	<url>
		<loc>http://www.asdf.com/</loc>
		<lastmod>2008-10-01</lastmod>
</url>
</urlset>
rizzo89 is offline   Reply With Quote
Old 12-31-2008, 09:36 PM   PM User | #6
dragonfly
New to the CF scene

 
Join Date: Sep 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
dragonfly is an unknown quantity at this point
Thanks for your solution ..

I was looking at techniques for embedding php code in xhtml files using a technique like this .. for svg ..

http://www.carto.net/papers/svg/samp..._svg_php.shtml


this would require a new mime type to be created in Apache mime.types file .. let's call it

.pxhtml .. similar to .psvg example

In fact you could embed svg in an xform


# ________________________________

# added new type pxhtml .. embedding php in xhtml files
# http://www.456bereastreet.com/archiv...html_properly/

application/xhtml+xml xhtml xht pxhtml

# ________________________________


But it seems that your code might offer a neater solution ..


also see here .. accepting XForms in PHP

http://www.ibm.com/developerworks/xm...php/index.html
dragonfly is offline   Reply With Quote
Old 01-22-2009, 06:19 PM   PM User | #7
rizzo89
New Coder

 
Join Date: Nov 2008
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
rizzo89 is an unknown quantity at this point
I have found myself trying to execute PHP tags within an XSL file again. I was told in another forum that this will do it.

In HTACCESS:

AddHandler application/x-httpd-php5 .xsl

It does not work for me.

I would like to place a "php includes statement" for my menus.

Code:
<ul>
<li><a href="/index.php?page=index">Home</a></li>
<li><a href="/index.php?page=pricing">Pricing</a></li>
<li><a href="/index.php?page=memos">Memos</a></li>
<li><a href="/index.php?page=forms">Forms</a></li>
<li><a href="/index.php?page=sales">Sales Support</a></li>	
</ul>
Code:
<?php include($_SERVER['DOCUMENT_ROOT'] . '/includes/menu.php'); ?>

What am I missing here?
rizzo89 is offline   Reply With Quote
Old 01-28-2009, 03:24 AM   PM User | #8
rizzo89
New Coder

 
Join Date: Nov 2008
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
rizzo89 is an unknown quantity at this point
Looks like it is better to use the XSL include for this application.

I am still looking for a solution to executing PHP in these templates??....BUMP!


This is the file that needs to be included.
Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template name="menu">
<ul>
<li><a href="/index.php?page=index">Home</a></li>
<li><a href="/index.php?page=pricing">Pricing</a></li>
<li><a href="/index.php?page=memos">Memos</a></li>
<li><a href="/index.php?page=forms">Forms</a></li>
<li><a href="/index.php?page=sales">Sales Support</a></li>	
</ul>
 </xsl:template>
</xsl:stylesheet>

File doing the including....
Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns="http://www.w3.org/1999/xhtml">
<xsl:include href="constant/mainTemplate.xsl" />
<xsl:output method="xml" omit-xml-declaration = "yes" indent="yes"/>

<xsl:template match="/">
<html>
<head>
<title></title>
</head>
<body>
<xsl:call-template name="menu"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
rizzo89 is offline   Reply With Quote
Reply

Bookmarks

Tags
addtype, php, xml, xsl, xslt

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:43 AM.


Advertisement
Log in to turn off these ads.