Archive for the 'PHP' Category

XSLT and PHP little trick

Hi all,

several days ago i’ve to use XSLT transformation in PHP code. One of the requirements is to use external .xml files, so in the .xsl file i have to use references like http://example.com/xml/myfile.xml, but sablotron doesn’t seems to implement this kind of schema. Only works with file: schema.

<xsl:value-of
select="document('http://example.com/xml/myfile.xml')
/items/group/item[./@code='ES']/@value"/>

this is a piece of XSLT that i want to use

I thought an update system that download the external resources, but, i can’t believe that didn’t exist any better solution (of course simpler).

My fist attemp was using xslt_base_dir with and url like:

$xh = xslt_create();
$fileBase = 'http://example.com/xml/';
xslt_set_base ( $xh, $fileBase );

… but didn’t work.

Checking all the XSLT methods in the PHP reference i’ve found one that seems fit: xslt_set_scheme_handlers. Despite it isn’t already documented one comment is enough explanatory.

function mySchemeHandler($processor, $scheme, $rest) {
 if($scheme == 'http') {
  return file_get_contents($scheme.":".$rest);
 }
}

$SchemeHandlerArray = array();
$SchemeHandlerArray["get_all"] = "mySchemeHandler";
			
$xh = xslt_create();
xslt_set_scheme_handlers($xh,$SchemeHandlerArray);
			
echo xslt_process($xh, 'arg:/_xml', $config['xslt'], NULL,
 array ( '/_xml' => utf8_encode($result) ), 
 array (	
  'link' => $link , 
  'locale' => $config['locale'] ,
  'extra' => $config['extra']
) );

xslt_free($xh);

and .. works!!

Java, PHP, XML and AJAX (how to mix all together)

Introduction

Why does anyone want to mix all this stuff together? are you crazy? maybe i am :)
The main purpouse of this experiment is to test the real availability of this heterogeneus environments working together.

First of all, i want to explain in a nutshell what i’m going to to and how.

I’ll use the powerfull platform of Java to implemente all the application logic. In this test, the application logic will be very simple, but in real scenarios maybe we want to use advanced features like replication, clustering, relational databases, work with XMLs documents, operations like XSL-T, XPath, XQuery, and so on. And there isn’t a better solution than Java.

With PHP you can code big projects, but it doen’t have all the Java third party APIs and the big comunity of the open source. But always is interesting to use the strengths of each other, and the web services is a way to connect both.

In Java i’m going to use Apache Axis to expose a web service and in PHP i’m going to use NuSOAP API to connect to this web service.

And the last step is to use AJAX to improve the use experience. In the Java side, the choice for me is very clear, i would use DWR (of course), but in PHP side i decided to use an agnostic Javascript library, i’m going to use Dojo Toolkit.

Java and PHP
This is a flow of whole process, how the several libraries and APIs interconnect.

Read more »