sander
sander

Reputation: 53

Processing custom XML namespace within XSL

I'm using the php class XSLTProcessor to generate HTML from a xsl. Within the xsl, I'd like all my custom namespace elements to be processed by my own processor class.

So for example:

<xsl:for-each select="doc/elements/*">
    <doc:renderElement element="." />
</xsl:for-each>

This should call the method renderElement of an instance of my custom processor class.

I know I can enable calling php functions by using the registerPHPFunctions function. However, this only seems to support calling static methods.

Upvotes: 3

Views: 459

Answers (2)

Welington Veiga
Welington Veiga

Reputation: 151

Well, I have a script using this, maybe it will be util for you

    // Simple XML
    $xml = new SimpleXMLElement($data);
     // You need to set every namespace
    $xml->registerXPathNamespace('e', 'http://myxml.withalotofnamespaces.com/xml');
    foreach ($xml->xpath('//e:responseData/e:cityCodes') as $city) {
       // getting each city as an Array,  $city is a SimpleXMLElement
       // Work Here
    }

If you want to try read in portuguese, here are a lot of other information for handle namespace XML using PHP with SimpleXMLFormt: http://www.plugandpray.com.br/posts/manipulando_xml_com_namespace_em_php

About SimpleXMLFormat http://php.net/manual/en/book.simplexml.php

Upvotes: -1

hakre
hakre

Reputation: 197795

Yes, XSLTProcessor::registerPHPFunctions only supports static function calls of classes. However you can make use of those static calls to provide a factory or instance registry to provide actual objects. In your case probably based on the element name.

Existing code where this is done can be found in the PIWI - PHP Transformation Framework.

Upvotes: 2

Related Questions