Solved

Applying XSL to XML using PHP

Posted on 2008-10-02
14
916 Views
Last Modified: 2012-05-05
I have downloaded some XML and an XSL file using eBay's API (through their developer program).

I want to use PHP to apply the XSL Transformation and print out the resulting HTML.

How can I do this?
0
Comment
Question by:sjamal_hsj
  • 8
  • 6
14 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 22630042
0
 
LVL 82

Expert Comment

by:hielo
ID: 22630052
0
 

Author Comment

by:sjamal_hsj
ID: 22630053
Hey,

I was actually on there earlier.

I'm on a shared server, and I don't know if everything is enabled correctly, but I've attached my code, and I get an error on live 17 ($proc->transformToURI($xml, 'converted.html');)

The errors are:

Warning: XSLTProcessor::transformToUri() [xsltprocessor.transformtouri]: xmlXPathCompOpEval: function node-set not found in /home/irfanj/public_html/scriptTests/getHTMLtest.php on line 17

Warning: XSLTProcessor::transformToUri() [xsltprocessor.transformtouri]: Unregistered function in /home/irfanj/public_html/scriptTests/getHTMLtest.php on line 17

Warning: XSLTProcessor::transformToUri() [xsltprocessor.transformtouri]: runtime error: file /home/irfanj/public_html/scriptTests/myAttributes.xsl line 4458 element variable in /home/irfanj/public_html/scriptTests/getHTMLtest.php on line 17

Warning: XSLTProcessor::transformToUri() [xsltprocessor.transformtouri]: Failed to evaluate the expression of variable 'HtmlSorceNode'. in /home/irfanj/public_html/scriptTests/getHTMLtest.php on line 17



What does all this mean?
<?
 
// Load the XML source
$xml = new DOMDocument;
$xml->load('test2.xml');
 
$xsl = new DOMDocument;
$xsl->load('myAttributes.xsl');
 
// Configure the transformer
$proc = new XSLTProcessor;
if (!$proc->hasExsltSupport()) {
    die('EXSLT support not available');
}
$proc->importStyleSheet($xsl); // attach the xsl rules
 
$proc->transformToURI($xml, 'converted.html');
 
?>

Open in new window

0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 82

Assisted Solution

by:hielo
hielo earned 500 total points
ID: 22630313
your syntax is right. The problem you are describing is discussed here:
http://php.chinaunix.net/manual/zh/function.xsl-xsltprocessor-has-exslt-support.php#51455
0
 

Author Comment

by:sjamal_hsj
ID: 22630340
Hey,

I tested the source files using the example script on php.net that you posted, and they worked fine.
So, as you said, the problem is definitely not my syntax.

I googled the error I was getting and came up with the same page you just posted, and ONLY that page, so I'm sorta stumped as to what to do next.

If the post on that page is a solution, I'm not experienced (with XML) to understand it.

So, I've posted my XML and XSL files, and I'd appreciate it if you could let me know what I need to do to solve the problem.

http://www.esentials.net/scriptTests/collection.xml
http://www.esentials.net/scriptTests/collection.xsl

thnx
0
 
LVL 82

Expert Comment

by:hielo
ID: 22630617
I just tried the xml/xsl files you posted with the script you pasted above (ID: 22630053) and it worked fine for me exactly as is, so the suggestion on the link above would not apply to you. I looked at the php site for configuration options, but there is nothing to configure. :(
0
 
LVL 82

Expert Comment

by:hielo
ID: 22630635
0
 

Author Comment

by:sjamal_hsj
ID: 22630769
I've tried that, but I get the following error:
Call to undefined function xslt_create()

Apparently, a lot of people are having the error, since that extension is no longer bundled with PHP 5 (what my hosting company is currently running).
http://gr2.php.net/manual/en/intro.xslt.php discusses the change in PHP5.

So, according to PHP.net, I should be using the original code (XSLTProcessor).
Also, I vaguely remember trying xslt_create on PHP4 and it didn't work correctly on my XML for some reason (something to do with the way eBay formats their XML and some version difference...I'm hazy on the details).

Here's my phpinfo():
http://www.esentials.net/scriptTests/phpinfo.php

I know you mentioned that you looked for something to configure and found nothing, but are there any related differences between our settings?

Thnx
0
 
LVL 82

Expert Comment

by:hielo
ID: 22630953
>>I know you mentioned that you looked for something to configure and found nothing
Yes, I looked under XSL runtime configuration:
http://us3.php.net/manual/en/book.xsl.php

>>but are there any related differences between our settings?
Hmm, we both have the same modules (libxml and xsl). My versions are a little older including the php version (Version 5.2.1). The only thing that "stands out" is that your configuration says:
'--with-xsl=/opt/xslt/'

mine simply says:
'--with-xsl'

Not sure  if that will help you.
0
 

Author Comment

by:sjamal_hsj
ID: 22635282
Hey,

>>I just tried the xml/xsl files you posted with the script you pasted above (ID: 22630053) and it worked fine
What was the HTML code it generated?

I ask because I found this
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_21861684.html)
and it mentions the exact same errors I have. So, I tried it with the script that the author posted at the end, and I DO get some sort of response, but its preceded by the same warnings, and its definitely not the entire HTML code.
I've posted my new PHP code, and I'm using the exact same XSL and XML files from post ID 22630340

thnx
<?php
 
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On');
ini_set('display_startup_errors', 'On');
 
// Load the XML source
$xml = new DOMDocument;
$xml->load('collection.xml');
 
$xsl = new DOMDocument;
$xsl->load('collection.xsl');
 
// Configure the transformer
//$proc = new XSLTProcessor;
//$proc->importStyleSheet($xsl); // attach the xsl rules
 
$proc = new XSLTProcessor();
$proc->registerPHPFunctions();
$xsl = $proc->importStylesheet($xsl);
 
$newdom = $proc->transformToDoc($xml);
print $newdom->saveXML();
 
?>

Open in new window

0
 

Author Comment

by:sjamal_hsj
ID: 22635344
I've taken a second look at the "warnings"...

It mentions an error on line 4457 element variable of my XSL file, and that corresponds to:
<xsl:variable name="HtmlSorceNode" select="x:node-set($HtmlSource)"/>

I didn't write the code and I'm no expert at this, but it seems to relate to the post you made (ID: 22630313) about the "node-set" problem.

If that is the problem, it leaves me with three questions:

1. Why did it work when you tested it?

2. How do I implement the solution described here:
http://php.chinaunix.net/manual/zh/function.xsl-xsltprocessor-has-exslt-support.php#51455

3. Why is this so difficult?
0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 22635697
Try changing:

... xmlns:x="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="x">
 
to:
xmlns:exslt="http://exslt.org/common" exclude-result-prefixes="exslt">
 
 
AND instead of:
<xsl:variable name="HtmlSorceNode" select="x:node-set($HtmlSource)"/>
 
you would need:
<xsl:variable name="HtmlSorceNode" select="exslt:node-set($HtmlSource)"/>

Open in new window

0
 

Author Comment

by:sjamal_hsj
ID: 22637041
I get the following result:
http://www.esentials.net/scriptTests/collectionWA.php

Warning: XSLTProcessor::transformToDoc() [xsltprocessor.transformtodoc]: xsltApplySequenceConstructor: fallback was not compiled

any ideas?
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 500 total points
ID: 22638112
>>What was the HTML code it generated?
See attached code

On another note, a "twist" to the suggestion I made before(ID: 22635697) would be to NOT get rid of:
xmlns:x="urn:schemas-microsoft-com:xslt"

leave it as is but instead ADD this as well:
xmlns:exslt="http://exslt.org/common"

and then change the exclude-prefixes to:
exclude-result-prefixes="x exslt"

AND lastly, instead of:
<xsl:variable name="HtmlSorceNode" select="x:node-set($HtmlSource)"/>

use:
<xsl:variable name="HtmlSorceNode" select="exslt:node-set($HtmlSource)"/>

AS for that last error you described, I don't have any suggestions :(
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head><body bgcolor="white"><form action="AttrPage" name="APIForm" id="APIForm" method="post">
<script LANGUAGE="JavaScript1.1"><!--
						function api_check_on_other(attributeId, attributeValue) {}
					//--></script>
				Select category:
				<br><select name="vcsid" onchange="javascript: document.forms['APIForm'].submit();"><option value="-10">--</option></select><input xmlns:exslt="http://exslt.org/common" xmlns:xal="http://xml.apache.org/xalan" type="hidden" name="aus_form_changed" value="default"><input xmlns:exslt="http://exslt.org/common" xmlns:xal="http://xml.apache.org/xalan" type="hidden" name="ButtonLoad" value="default">
</form></body>

Open in new window

0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
preventing spam with mailto 22 36
mysqli insert query problems 4 22
Presenting past 6 months bug 7 30
Moving from Mcrypt to OpenSSL 15 25
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

832 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