我使用php类XSLTProcessor从xsl生成HTML。在xsl中,我希望所有自定义名称空间元素都由我自己的处理器类处理。
例如:
<xsl:for-each select="doc/elements/*">
<doc:renderElement element="." />
</xsl:for-each>
这应该调用我的自定义处理器类实例的方法renderElement。
我知道我可以通过使用registerHPFunctions函数来调用php函数。然而,这似乎只支持调用静态方法。
XSLTProcessor::registerPHPFunctions
只支持类的静态函数调用。但是,您可以使用这些静态调用来提供工厂或实例注册表来提供实际对象。在您的情况下,可能基于元素名称。
现有的代码可以在PIWI-PHP转换框架中找到。
好吧,我有一个使用这个的脚本,也许它对你有用
// 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
}
如果您想尝试用葡萄牙语阅读,以下是使用SimpleXMLFormt的PHP处理命名空间XML的许多其他信息:http://www.plugandpray.com.br/posts/manipulando_xml_com_namespace_em_php
关于SimpleXMLFormathttp://php.net/manual/en/book.simplexml.php