{"id":220,"date":"2007-07-14T12:03:39","date_gmt":"2007-07-14T12:03:39","guid":{"rendered":"https:\/\/virtualchaos.co.uk\/blog\/2007\/07\/14\/calling-php-functions-from-xsl\/"},"modified":"2007-07-14T12:07:37","modified_gmt":"2007-07-14T12:07:37","slug":"calling-php-functions-from-xsl","status":"publish","type":"post","link":"https:\/\/virtualchaos.co.uk\/blog\/2007\/07\/14\/calling-php-functions-from-xsl\/","title":{"rendered":"Calling PHP Functions from XSL"},"content":{"rendered":"<p><a href=\"http:\/\/gallery.craigharris.co.uk\/main.php\">Craig<\/a>, a colleague of mine who newly joined our development team at Talis showed me this neat little trick. Many things are far easier to do in PHP than they are in XSL, and some things simply can&#8217;t be done in pure XSL. A solution is to call PHP functions directly from within your XSL.<\/p>\n<p>1) In your xsl stylesheet add:<\/p>\n<p><pre>\r\n   namespace xmlns:php=\"http:\/\/php.net\/xsl\"\r\n   exclude-result-prefixes=\"php\"\r\n<\/pre>\n<\/p>\n<p> 2) To call the php function and access the result use:<\/p>\n<div class=\"dean_ch\" style=\"white-space: wrap;\">\n<ol>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp;<\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp; <span class=\"sc3\"><span class=\"coMULTI\">&lt;!&#8211; for string use this &#8211;&gt;<\/span><\/span><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp; <span class=\"sc3\"><span class=\"re1\">&lt;xsl:value-of<\/span> <span class=\"re0\">select<\/span>=<span class=\"st0\">&quot;php:functionString(&#8216;phpFunctionName&#8217;, \/xpath)&quot;<\/span><span class=\"re2\">\/&gt;<\/span><\/span><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp;<\/div>\n<\/li>\n<li class=\"li2\">\n<div class=\"de2\">&nbsp; <span class=\"sc3\"><span class=\"coMULTI\">&lt;!&#8211; for DOM Nodes use this &#8211;&gt;<\/span><\/span><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp; <span class=\"sc3\"><span class=\"re1\">&lt;xsl:copy-of<\/span> <span class=\"re0\">select<\/span>=<span class=\"st0\">&quot;php:function(&#8216;phpFunctionName&#8217;, \/xpath)&quot;<\/span><span class=\"re2\">\/&gt;<\/span><\/span><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp;<\/div>\n<\/li>\n<\/ol>\n<\/div>\n<p>You can pass as many parameters as you want to either php:function or php:functionString &#8211; the latter merely converts output to a string and otherwise they are identical.\n<\/p>\n<p>\n3) you must register them with the XSL Transformer:<\/p>\n<div class=\"dean_ch\" style=\"white-space: wrap;\">\n<ol>\n<li class=\"li1\">\n<div class=\"de1\">span class=&#8221;co1&#8243;>\/\/ This is the important call for this functionality<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<p>4) In your php function, access parameters passed in as strings as if they are a php string. If you pass a dom structure as a parameter then you need to access it along the lines of:<\/p>\n<div class=\"dean_ch\" style=\"white-space: wrap;\">\n<ol>\n<li class=\"li1\">\n<div class=\"de1\">span class=&#8221;st0&#8243;>&#8217;namespace&#8217;<\/span>, <span class=\"st0\">&#8216;element-name&#8217;<\/span<\/div>\n<\/li>\n<\/ol>\n<\/div>\n<p>$DomList will include the root element of the XPath used to call the PHP function<\/p>\n<p>If you want to dump what you pass to PHP as a string you need to do: <\/p>\n<div class=\"dean_ch\" style=\"white-space: wrap;\">\n<ol>\n<li class=\"li1\">\n<div class=\"de1\">span class=&#8221;co1&#8243;>\/\/ note this function isn&#8217;t yet documented in the PHP manual !<\/span><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp;<span class=\"br0\">&#125;<\/span><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp;<\/div>\n<\/li>\n<\/ol>\n<\/div>\n<p>It&#8217;s a very useful feature &#8230; good luck with it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Craig, a colleague of mine who newly joined our development team at Talis showed me this neat little trick. Many things are far easier to do in PHP than they are in XSL, and some things simply can&#8217;t be done &hellip; <a href=\"https:\/\/virtualchaos.co.uk\/blog\/2007\/07\/14\/calling-php-functions-from-xsl\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[404,124,139],"class_list":["post-220","post","type-post","status-publish","format-standard","hentry","category-development","tag-development","tag-php","tag-xsl"],"_links":{"self":[{"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/posts\/220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=220"}],"version-history":[{"count":0,"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/posts\/220\/revisions"}],"wp:attachment":[{"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}