Archive for the 'XSLT' Category

A simple debug XSLT

For debug purposes i want to output the input xml document inside a textarea. This is a easy way to select and copy the xml and use later in other application.

So, here it is:

<xsl:stylesheet 
 version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output 
 method="html" 
 encoding="ISO-8859-1" 
 indent="yes"
 omit-xml-declaration="yes"  
 media-type="text/html"/>
 
<xsl:template match="node()|@*">
 <xsl:copy>
  <xsl:apply-templates select="node()|@*"/>
 </xsl:copy> 
</xsl:template>
				
<xsl:template match="/">
 <textarea cols="80" rows="20">
  <xsl:apply-templates select="node()|@*"/>
 </textarea>
</xsl:template>
	
</xsl:stylesheet>

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!!