3

I need to extend XSLT processor in PHP with my own tags and functions. I found some APIs, but these APIs is still not what I need.

1) http://php.net/manual/ru/book.xsl.php - quite simple API, but I can extend functions only (registerPHPFunctions), not tags.

2) http://www.php.net/manual/en/ref.xslt.php - nice and simple, I can extend whatever I need (xslt_set_sax_handlers), but, here we go - another problem. Introduction (http://www.php.net/manual/en/intro.xslt.php) says: "This extension has been moved to the » PECL repository and is no longer bundled with PHP as of PHP 5.0.0.". Okay, I'm navigating to pecl.php.net -> Search Packages -> XSLT -> No results found -> o_O

So, looks like the good API was deprecated, and the bad API doesn't satisfy my needs. What can you recomend guys? I need a PHP XSLT processor where I can extend or override everything that can be extended or overridden like the "2)" above does. Thanks!

nyan-cat
  • 610
  • 5
  • 19
  • It would be better if you said what you are trying to achieve. You want to add extension elements to XSLT, I take it, but why exactly? – barefootliam Sep 13 '12 at 01:39
  • Yeah, I'm interested in hearing your functional requirements also. PHP has quite a lot of nice XML parsing functionality to complement the usual SAX/DOM/XSL features. You may find that what you need is already there unless your solution absolutely must be XSL. – Mike Girard Sep 13 '12 at 09:20
  • Hey guys. I'm writing new version of my website engine. The essense of the engine - is XSLT processor + tag/function extensions + transparent data binding (like MySQL). So, I need a way to write custom PHP handlers for several XML tags. Currently I use regexp replace to achive custom XML tags behavior, but it has a set of problems. For example - nested structures handling. – nyan-cat Sep 13 '12 at 10:02
  • possible duplicate of [Upgrade PHP XSLT processor to XSLT 2.0](http://stackoverflow.com/questions/3873996/upgrade-php-xslt-processor-to-xslt-2-0) – Paul Sweatte Apr 08 '14 at 02:10

1 Answers1

0

PHP uses libxslt as its default processor, and libexslt as its extension processor. Here is the API documentation:

In addition, libxsltalready supports an eval extension, which may serve your purposes.

There's also built-in macros available via xsl:processing-instruction which match the shorthand delimiters of PHP:

And the registerPHPFunctions method.

A transformation can pass a XPath expression as an argument to a function in a custom namespace:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:js="urn:livescript"
  extension-element-prefixes="js"
>

  <xsl:variable name="source" select="//"/>

  <xsl:template match="json">
    <xsl:value-of select="livescript:JSON($source)"/>
  </xsl:template>
</xsl:stylesheet>

The function links to an external transformation which computes the XPath result and returns the result by using a callback.

References

Community
  • 1
  • 1
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265