Using PHP in XSL
Can PHP be used in XSL document?
Always when I try to do this I get errors ... so before I worry I would like to know if this is possible. (I am an absolute beginner)
I have an XSL file like this
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title></title>
<style type="text/css">
[...]
</style>
</head>
<body>
[...]
<div id="content">
<?php echo $anything; ?>
</div>
[...]
</body>
</html>
</xsl:template>
</xsl:stylesheet>
(I cut out the code)
So I am including an XML file via PHP (this XML file is styled with this XSL file) And now I tried to echo the content like $ anything
But it doesn't work
If you will be using the XSLTProcessor class to execute XSL, you can simply registerPHPFunctions . I do this all the time for certain data manipulations in XSL. Then I can call any PHP function or method I want in XSL.
source to share
If you are using a Saxon-EE 9.7 XSLT processor, you can use <xsl:processing-instruction name="php">
as a tag <?php
with one fancy warning: you have to add ?
just before the closing tag </xsl:processing-instruction>
.
<xsl:processing-instruction name="php">
echo "hello world!";
?</xsl:processing-instruction>
To use your example, it would look like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title></title>
<style type="text/css">
[...]
</style>
</head>
<body>
[...]
<div id="content">
<xsl:processing-instruction name="php">
echo $anything;
?</xsl:processing-instruction>
</div>
[...]
</body>
</html>
</xsl:template>
</xsl:stylesheet>
source to share
You can use simplexml to manipulate XML in PHP. http://nl.php.net/simplexml there is a link of the simplexml class. Therefore, after loading the XML file into PHP and before repeating it with the asXML () function, you can modify the XML through the simplexml interface.
source to share