[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

how to use xml and xslt file in php?

Posted on 2006-05-23
15
Medium Priority
?
988 Views
Last Modified: 2013-01-08
would some one help me to use xml and xslt file in php. my job is to display html content using the xml and xslt file. How can i do this. i get a piece of code i tried myself nothing can be displayed. pls help me.

<?php

// Allocate a new XSLT processor
$xh = xslt_create();

// Process the document, returning the result into the $result variable
$result = xslt_process($xh, 'sample.xml', 'sample.xsl');
if ($result) {
    print "SUCCESS, sample.xml was transformed by sample.xsl into the \$result";
    print " variable, the \$result variable has the following contents\n<br>\n";
    print "<pre>\n";
    print $result;
    print "</pre>\n";
}
else {
    print "Sorry, sample.xml could not be transformed by sample.xsl into";
    print "  the \$result variable the reason is that " . xslt_error($xh) .  
    print " and the error code is " . xslt_errno($xh);
}

xslt_free($xh);

?>
0
Comment
Question by:jessi_vs
  • 7
  • 6
  • 2
15 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 16748925
Do you get blank screen or do you get errors?

Have you made sure that the XSLT extension is active?

Try adding to the top of your file:
<?php
  error_reporting(E_ALL);


-r-
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 16748937
Do you have XML and XSL support enabled in PHP?
You can check this by creating the following file:
<?php
  phpinfo();
?>

See if you can find any references to XML and XSL when you run the file from your webserver...
0
 

Author Comment

by:jessi_vs
ID: 16748991
I am gettting a blank page, after include the  error_reporting(E_ALL); alse i am getting the same. If run phoinfo() i get the following under xml and xsl.

XML
XML Support  active  
XML Namespace Support  active  
libxml2 Version  2.6.22  

XSL
XSL  enabled  
libxslt Version  1.1.15  
libxslt compiled against libxml Version  2.6.22  
EXSLT  enabled  
libexslt Version  0.8.12  

Thanks
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
LVL 49

Expert Comment

by:Roonaan
ID: 16749039
If you get a blank space there is most likely a syntax error or other fatal error.

-r-
0
 

Author Comment

by:jessi_vs
ID: 16749196
i am using php 5.1.4. I could not find php_xslt.dll in my ext folder and there is no entry in php.ini file as well. I have downloaded the dll and include the line under extension. but there is no xslt information while running phpinfo().
0
 
LVL 29

Accepted Solution

by:
TeRReF earned 200 total points
ID: 16749347
Add these lines to the top of your php document and show us the error message:

        ini_set('error_reporting', E_ALL);
        ini_set('display_errors', 'On');
        ini_set('display_startup_errors', 'On');

0
 
LVL 29

Expert Comment

by:TeRReF
ID: 16749353
By the way,
don't forget to restart your webserver if you change anything in your php.ini!
0
 

Author Comment

by:jessi_vs
ID: 16749366
after put those lines finally i got the error

Fatal error: Call to undefined function xslt_create()

Thanks.
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 16749520
Did you get xsl running yet?
0
 

Author Comment

by:jessi_vs
ID: 16749662
while using phpinfo() i am getting the following info under xsl

XSL
XSL  enabled  
libxslt Version  1.1.15  
libxslt compiled against libxml Version  2.6.22  
EXSLT  enabled  
libexslt Version  0.8.12  

Thanks

0
 
LVL 29

Expert Comment

by:TeRReF
ID: 16749934
PHP 5 uses other functions i.e.:
$xslt = new XSLTProcessor;

Have a look here:
http://php.net/xsl
0
 

Author Comment

by:jessi_vs
ID: 16750257
I HAVE tested using the following php code, i am getting output

<?php
$xml = <<<EOB
<allusers>
 <user>
  <uid>bob</uid>
 </user>
 <user>
  <uid>joe</uid>
 </user>
</allusers>
EOB;$xsl = <<<EOB
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:php="http://php.net/xsl">
<xsl:output method="html" encoding="utf-8" indent="yes"/>
 <xsl:template match="allusers">
  <html><body>
   <h2>Users</h2>
   <table>
   <xsl:for-each select="user">
     <tr><td>
       <xsl:value-of
             select="php:function('ucfirst',string(uid))"/>
     </td></tr>
   </xsl:for-each>
   </table>
  </body></html>
 </xsl:template>
</xsl:stylesheet>
EOB;$xmldoc = DOMDocument::loadXML($xml);
$xsldoc = DOMDocument::loadXML($xsl);

$proc = new XSLTProcessor();
$proc->registerPHPFunctions();
$proc->importStyleSheet($xsldoc);
echo $proc->transformToXML($xmldoc);
?>


==
my problem is how to put my xml and xsl files. here they embeded it.

Thanks for your reply.
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 16756119
Use file_get_contents:
http://php.net/file_get_contents
0
 

Author Comment

by:jessi_vs
ID: 16783474
I have found the way to use xml and xsl:

<?php

ini_set('error_reporting', E_ALL);
        ini_set('display_errors', 'On');
        ini_set('display_startup_errors', 'On');

/* load the xml file and stylesheet as domdocuments */
$xsl = new DomDocument();
$xsl->load("2.xsl");
$inputdom = new DomDocument();
$inputdom->load("2.xml");

/* create the processor and import the stylesheet */
$proc = new XsltProcessor();
$xsl = $proc->importStylesheet($xsl);
//$proc->setParameter(null, "user", "user");

/* transform and output the xml document */
$newdom = $proc->transformToDoc($inputdom);
print $newdom->saveXML();
?>

right now i am getting the following error in the following line;
$newdom = $proc->transformToDoc($inputdom);

Warning: XSLTProcessor::transformToDoc() [function.XSLTProcessor-transformToDoc]: xsltExtFunctionTest: PHP Object did not register PHP functions in c:\Inetpub\wwwroot\phptest\xmltest1.php on line 19

Warning: XSLTProcessor::transformToDoc() [function.XSLTProcessor-transformToDoc]: xmlXPathCompiledEval: evaluation failed in c:\Inetpub\wwwroot\phptest\xmltest1.php on line 19

Warning: XSLTProcessor::transformToDoc() [function.XSLTProcessor-transformToDoc]: runtime error: file file:///c%3A/Inetpub/wwwroot/phptest/2.xsl line 13 element value-of in c:\Inetpub\wwwroot\phptest\xmltest1.php on line 19

Warning: XSLTProcessor::transformToDoc() [function.XSLTProcessor-transformToDoc]: xsltValueOf: text copy failed in c:\Inetpub\wwwroot\phptest\xmltest1.php on line 19

Warning: XSLTProcessor::transformToDoc() [function.XSLTProcessor-transformToDoc]: xsltExtFunctionTest: PHP Object did not register PHP functions in c:\Inetpub\wwwroot\phptest\xmltest1.php on line 19

Warning: XSLTProcessor::transformToDoc() [function.XSLTProcessor-transformToDoc]: xmlXPathCompiledEval: evaluation failed in c:\Inetpub\wwwroot\phptest\xmltest1.php on line 19

Warning: XSLTProcessor::transformToDoc() [function.XSLTProcessor-transformToDoc]: runtime error: file file:///c%3A/Inetpub/wwwroot/phptest/2.xsl line 13 element value-of in c:\Inetpub\wwwroot\phptest\xmltest1.php on line 19

Warning: XSLTProcessor::transformToDoc() [function.XSLTProcessor-transformToDoc]: xsltValueOf: text copy failed in c:\Inetpub\wwwroot\phptest\xmltest1.php on line 19

Fatal error: Call to a member function saveXML() on a non-object in c:\Inetpub\wwwroot\phptest\xmltest1.php on line 20

Thanks.



0
 

Author Comment

by:jessi_vs
ID: 16839928
thank you all finally i have fixed the problem

<?php

            ini_set('error_reporting', E_ALL);
        ini_set('display_errors', 'On');
        ini_set('display_startup_errors', 'On');

/* load the xml file and stylesheet as domdocuments */
$xsl = new DomDocument();
$xsl->load("xsl file/test.xsl");
$inputdom = new DomDocument();
$inputdom->load("XML File/test.xml");

/* create the processor and import the stylesheet */

$proc = new XSLTProcessor();
$proc->registerPHPFunctions();
$xsl = $proc->importStylesheet($xsl);

//$proc->setParameter(null, "cd", "cd");

/* transform and output the xml document */
$newdom = $proc->transformToDoc($inputdom);
print $newdom->saveXML();
?>
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question