XSL

add a note add a note

User Contributed Notes 23 notes

up
3
rojaro
13 years ago
If you're want to use XML from a variable e.g. $xmldata but the XSLT-StyleSheet from a file, you can do it also the following way:

<?php
$xslt
= new xsltProcessor;
$xslt->importStyleSheet(DomDocument::load('filename.xsl'));
print
$xslt->transformToXML(DomDocument::loadXML($xmldata));
?>
up
1
appletalk at gmail dot com
12 years ago
As many of you may have noticed, DOM parser gives errors if the '&nbsp;' entity is present. The E_WARN message looks like:

Warning: DOMDocument::load() [function.load]: Entity 'nbsp' not defined in ...

There're many ways to solve this:
a) The hard way
<xsl:text disable-output-escaping="yes"> &amp;nbsp;</xsl:text>

b) Defining &nbsp;
At the top of the document, after the <?xml?> definition, add:
   <!DOCTYPE xsl:stylesheet [
    <!ENTITY nbsp "&#160;" >
    ]>

c) External Doctype
Just in case you want need other HTML entities, you can call an external doctype with the proper definitions

<!DOCTYPE page SYSTEM "http://gv.ca/dtd/character-entities.dtd">

Of course, you can download the file and place it in your server.
up
1
mnot at mnot dot net
10 years ago
You can set an HTTP proxy for the XSLT document() function to use (as well as DTD external references) by setting http_proxy in the environment.

E.g., in Apache configuration;

SetEnv http_proxy http://127.0.0.1:3128/
up
1
adrian at foeder dot de
11 years ago
If you want to use the document()-function inside your XSL stylesheet, please note that you have to use an absolute path there.
Important for windows users: the absolute path *has* to be with forward slashes, so subsitute windows-path-backslashes to forward slashes before you transform the document.

Examples:
This will NOT work:
<xsl:copy-of select="document('test.xml')" />

This will also NOT work:
<xsl:copy-of select="document('c:\temp\test.xml')" />

But this WILL work:
<xsl:copy-of select="document('c:/temp/test.xml')" />
up
1
Anonymous
11 years ago
In response to appletalk at gmail dot com
<snip>
As many of you may have noticed, DOM parser gives errors if the '&nbsp;' entity is present. The E_WARN message looks like:
Warning: DOMDocument::load() [function.load]: Entity 'nbsp' not defined in ...
There're many ways to solve this:
.....
b) Defining &nbsp;
At the top of the document, after the <?xml?> definition, add:
   <!DOCTYPE xsl:stylesheet [
   <!ENTITY nbsp "&#160;" >
   ]>
.......</snip>
Just wanted to let people know that option b does NOT work. I'm not sure why this isn't implemented correctly, but it isn't, so don't waste your time. It's unfortunate the DOMXSL transform is so much less capable than the old xslt function.
up
1
rd at jerntorget dot se
13 years ago
If you get the following warning message:

xsltApplyOneTemplate: _if_ was not compiled in (if can for example be apply-template or some other method), i've found that the problem seems to be an &nbsp; directly before the <xsl:if> or what ever is causing the problem.

One way to get thru the problem is to use span tags around &nbsp;  like : <span>&nbsp;</span>
up
0
emilise dot victor at gmail dot com
9 years ago
Here is a way to get an output without using <xml:message> : use the var_dump function of php :
- First bind the php functions with
<? $xslt->registerPHPFunctions() ?>
and xmlns:php="http://php.net/xsl" in the stylesheet
- Then you can call the var_dump function like this : <xsl:variable name="a" select="php:function('var_dump', $return)" />

I couldn't find a way to call the function and have it really print the result without creating a variable, but well, it's just for debugging...
up
0
pb at online-magazin dot at
10 years ago
Calling the Saxon XSLT Processor is a very esay task to do!

You just need to do some simple task

1. Install the JavaBridge for PHP

2. Download the freeware (B) Saxon distribution from http://saxon.sourceforge.net/

3. Put the jar files in a directory whre you have access

4. Use this sample of code

// Directory where the jar files are located
define("SAXON_DIR", $_SERVER['DOCUMENT_ROOT']."/saxonb8.9.0/");

// include the jars
java_require(SAXON_DIR."saxon8.jar;".SAXON_DIR."saxon8-dom.jar");


$sXslFile = $_SERVER['DOCUMENT_ROOT']."/myfirst.xsl"; // The xsl file
$sXmlFile = $_SERVER['DOCUMENT_ROOT']."/myfirst.xml"; // The xml file

try
{
  $oXslSource = new java("javax.xml.transform.stream.StreamSource", "file://".$sXslFile);
  $oXmlSource = new java("javax.xml.transform.stream.StreamSource", "file://".$sXmlFile);

  $oFeatureKeys = new JavaClass("net.sf.saxon.FeatureKeys");

  // Create the Factory
  $oTransformerFactory = new java("net.sf.saxon.TransformerFactoryImpl");

  //Disable source document validation
  $oTransformerFactory->setAttribute($oFeatureKeys->SCHEMA_VALIDATION, 4);

  // Create a new Transformer
  $oTransFormer = $oTransformerFactory->newTransformer($oXslSource);
   
  // Create a StreamResult to store the output
  $oResultStringWriter = new java("java.io.StringWriter");
  $oResultStream = new java("javax.xml.transform.stream.StreamResult", $oResultStringWriter);
 
  // Transform
  $oTransFormer->transform($oXmlSource, $oResultStream);
 
  // Echo the output from the transformation
  echo java_cast($oResultStringWriter->toString(), "string");
 
}
catch(JavaException $e){
   echo java_cast($e->getCause()->toString(), "string");
   exit;
}

5. Enjoy

This is working quite well.
up
0
Vasil Rangelov
10 years ago
@basslines at gmail dot com
"
As far as I can tell, the most recent stable versions of LibXML/LibXSLT/LibEXSLT do NOT support xPath 2.0 / XSLT 2.0 transformations. The only support for XSLT 2.0 that I've found is in Java's SAXON processor (http://saxon.sourceforge.net/).
"

Using SAXON or AltovaXML by using their JAVA bindings could prove to be a difficult task to do, and .NET seems to be buggy (at least for me). However, I've made a class that uses SAXON or AltovaXML from the command line. AltovaXML could also be used with COM, provided it's registered as a COM component.

You can download my wrapper from:
http://xslt2processor.sourceforge.net/

I still need to implement a more graceful error handling (the way the XSL extension uses the Libxml functions), and once the bug
http://bugs.php.net/bug.php?id=41577
is fixed, I'll implement the .NET interfaces too.

I'll appreciate any feedback.
up
0
geoffreyj dot lee at gmail dot com
10 years ago
Here are a couple useful tips for those who care about cross-browser compatibility.

Given:

<script type="text/javascript" src="test.js"></script>

XSLT will automatically condense it to:

<script type="text/javascript" src="test.js"/>

While this is good, Internet Explorer breaks on empty script tags. So, the simple solution is to add something to prevent an empty tag. For example:

<script type="text/javascript" src="test.js"><xsl:comment/></script>

Produces:

<script type="text/javascript" src="test.js">

</script>

------------------------

Also, here is a way to use IE's conditional comments:

<xsl:comment>[if gte IE 5]&#62;
&#60;link rel="stylesheet" type="text/css" href="ie.css" /&#62;
&#60;![endif]</xsl:comment>

Produces:

<!--[if gte IE 5]>
<link rel="stylesheet" type="text/css" href="ie.css" />
<![endif]-->

This will let you isolate IE-related CSS without needing to use ugly CSS hacks.
up
0
gabriel dot birke at web dot de
10 years ago
Each <xsl:message> tag will generate a PHP error (level Warning). If you want to collect and pretty-print them, you could use a custom error handling function that collects the warnings in a static variable.
up
0
Anonymous
11 years ago
Enable libxslt library, PHP 5, under windows:

To Enable:
In your php.ini
1. Uncomment ;extension=php_xsl.dll
2. Change extension_dir to
     extension_dir = "./ext"

To Confirm:
1. In a test.php page:
<?php phpinfo() ?>
2. Run test.php
3. Search for "libxslt Version". It should return a version number in a XSL headed table.

Further info Google
"Configuring and Testing PHP Servers for XSL Support"
up
0
Fabrice Bonny
11 years ago
In response to how to use entities from DTD (internal or external) in XSLT. It works if you do this way:

$xsl = new DOMDocument;
$xsl->resolveExternals = TRUE;
$xsl->substituteEntities = TRUE;
$xsl->load(...);

Hope this helps! ;-)
up
0
venkatesh at lammersmedical dot com
12 years ago
Here is function to read from the XSL sheet which is saved as a text file.

function ReadExcelSheet($filename){
             $test=file($filename);
             $ar1=str_replace("~[^\t]*\t","\t",$test);
             $ar2=str_replace("~","",$ar1);
             $ar=str_replace("","",$ar2);
             $temp=array();
             for ($i=0; $i<count($ar); $i++) {
                       if((substr($ar[$i],0,1)!= "\t")){
                           if($ar[$i]!=="\r\n"){
                           array_push($temp,$ar[$i]);
            }
        }
    }
    $name=split("\t",$temp[0]);
    $ExcelList=array();
    for($i=1;$i<count($temp);$i++){
        $split_result=split("\t",$temp[$i]);
        array_push($ExcelList,$split_result);
    }
    $result=insert_into_array($ExcelList,0,$name);
    return($result);
}
  $oResultStream = new javamp;votos of LibXML/LibXSLT/LibEXSLT drtrong>
ng <f Libelatmo4,0-.php'i0-dstemp[0] I sphref="htbetry-&p; truct&np; v> dd>
<&nbsn yos far asit_dumpstron ( />3maybe 4s)ode bsp; ar>&lyext/jssasea difsbsp;' entity isrd">-&dynixer1=syn
In yourtocompiled in (
rror (imil
just need to do href.org/eful error handling (the way the XSL ext href.org/i.phpTYPE arnt2pro>I couldn't find a way to call the functi>    $result=ins541 will let you isolate IE-related CSS wits541 wimanual/vote-note.php?id=48416
jw clasws">sy" id="V down>
down< ;     &nb541 wildiv> rojaro&ML fromer If you're want to use XML fass="html"> I />     exten(
To5Document
et from a file, you can do it als_t="pher />     extensio; }
    $name=split(/span>
    />}
cat    &50472"> et from a file, you can do it als_br />((((
(((((    $name=split(Document((((((((($xslt    $name=split(Document$xslt->$ExcelList=array();
  &nbrd">(
$xslt->$ExcelL/>  //Di;   $name=split(Document(((((xsltProcessor;
$uls"sing <xml:message> : usr />
  //Di;   $name=split(Document
(    $name=split(    &oreachpord">(xslt.removeparameteSheet(((                    & }
    $name=split(Document
((xslt'));
print

  //Di;   $name=split(Document
t/>  //Di;   $name=split(Document(
(;
$uls">
(    &} elseo; }
    $name=split(    /span>(
    &}/>
  //Di;   />}
cat    &50472"> et from a file, you can do it als_veryToXML
(    $name=split(un.reord">(    />}
}
catrd">(
;
;
((     extensiclass="default"><?php
$xslt <0-ds e.g. $xmldata but the XSLT-Stylrd">(
(((( I />     extenr />
&nbsrd">(
(
<?php
$xslt <0-dsn>->$ExcelL/>&nbsdefault"><?php
$xslt >loadXML
(    $result=in1045uteEntities = TRUE;
$xsl->load(..1045uteEmanual/vote-note.php?id=48416
Jaroslaw Zabiello down> down< ;     &n1045uteEndiv> 11-06-23> emilise 6g class="user">rojaro rPHP ea errnbspremovePars3aode/span>s  // ed ask />&nbs hp_xsl.dll
2. Change extension_ />&nbsdefault"><?php
&nbs * @remov9documeuls" />&nbs * @/span>< rPHP lt" />&nbs */ />&nbsdefault"><?php
et from a file, you can do itnbsp; // class="keyword">));
print (
xsltProcessor;
<askEtp://x;
$uls"sing <xml:message> : usr } he top of thbsp; &n/span>(xslt'<askEtp://x;
$ult"sing <xml:message> : usr } he top} />&nbsdefault"><?php
$xslt >loadXML
(    $result=in9051te the DOMXSL transform is so much less 9051te manual/vote-note.php?id=48416
sudhirss="tal-08-21 07:31"> down< ;     &n9051te tdiv> 9" cla5ser"4em>emilise 8g class="user">rojaro
(load/ToXML<?php
$xslt trans(Document
(<?php
$xslt trans(Document
(
load/ToXML<?php
<?php
$xslt task e.g. $xmldata but the XSLT-StyleSheet from a file, you can do it class="htmlowing way:

<?php
$xslt taskToXML
xsltansformToXML(<?php
<?php
$xslt tl to e.g. $xmldata but the XSLT-StyleSheet from a file, you can do it
<?php
$xslt taSk e.g. $xmldata but the XSLT-StyleSheet from a file, you can do it class="htmlowing way:

<?php
$xslt taSkToXML
xsltansfToXML(xsltLIBorm_NOCDATADocument&nbsDocumentxsltProcessor;
<(
<?php<?php
$xslt xslt'(loadXML(    $result=in8585teEntities = TRUE;
$xsl->load(..8585teEmanual/vote-note.php?id=48416
al-hp?77id="V81755" title="50% like this..."> 0 585teEnef="#81755" class="name"> emilise dot victor at gmail dot com
&website, clasly dust12 />&nedious LibXML/nuctbuildrefs.ivask treiv croughn class="htmpe="="pheEtp://xsl/>3L frndChild>/nal/>&website />&nuct ript"dumpask aodeiv> (/stron <xs"2008 uperil"> ystemt12 xt" ir />ipt"oxon disHTML) ; tag&nbs n class=stroe">r (lrucsaxot12 ask e">r treivconan classources="b007-0ms tot-24 At;
="http://p//p cli, You can downlo.eful error handling (the way the XSL extdomi/>You can downlo.1755">(    $result=in63738her info Google
"Configuring and Test38hemanual/vote-note.php?id=63764&page=boo38/div>
le="2007-08-21 07:31"> 0 38her/div> Fabrice Bonny
@basslines at gmail dot com
"
As far as I can tell, the most recent stable versions of LibXML/LibXSLT/LibEXSLT do NOT support xPath 2.0 / XSLT 2.0 transformations. The only support for XSLT 2.0 that I've f>(    $result=in74883will let you isolate IE-related CSS with4883wimanual/vote-note.php?id=63764&page=h4883/div>
Ry/diD. H Echitle="50% like this..."> 0 14">gabriel dot birke at web dot de "text" ispan cecefo Ws ago Enastro iv> Ebr />(    $result=in9039 will let you isolate IE-related CSS wit9039 wimanual/vote-note.php?id=48416
n cen-08-2no down< ;     &n9039 wildiv> 9" cl19 08:45m>emilise 8g class="user">rojaro1. GetearchNetbeil > Each 10 03:1r />1. F">rourcefo: C.php 'l to a DML/Li't kecent;' entity isScript tr />1.  
giv7-0/spat-24 03:58">1. for t; tup: Ule="20TYPEor idne usai 06:26">'./ode>32-ispan cer.msis="ainlass=""gpe="ime se55">://saxo " target="_bl!it(    $result=in80348her info Google
"Configuring and T80348hemanual/vote-note.php?id=48416
s"> Exiao08-2on_d1"> 0 0348heref="#81755" class="name"> ng <f Libelatmo4,0-.php'i0-dstempgt;OM api="text/jls" to be ent. l="nof
10 k hrogniseot-2son>OM nbel"p;' entity isEgr />1. s if you "="pheEtp://xNS('SLT/LibEXSLT do www.w3.org/1999/(foot">l/vote-note.php?ixt/416
/div>
re limg type'/im dth='12' height='12'>">dmanu>xt/ja 64&ait1755">/spanitvote=d