1

I have an issue of using PHP 5's XSLTProcessor to manipulate the following XML document. The issue is not the output being processed by the XSLT but elements of the html output are getting xmlns:php="http://php.net/xsl" added to them. An example is below.

The PHP:

$xmldoc = DOMDocument::load($xml);
$xsldoc = DOMDocument::load($xslt);
$proc = new XSLTProcessor();
$proc->registerPHPFunctions();
$proc->importStyleSheet($xsldoc);
echo $proc->transformToXML($xmldoc);

The XML:

<allusers>
 <user>
  <uid>bob</uid>
 </user>
 <user>
  <uid>tom</uid>
 </user>
</allusers>

The XSLT:

 <?xml version="1.0"?>
 <xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:php="http://php.net/xsl"
  xsl:extension-element-prefixes="php">

 <xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>

 <xsl:template match="allusers">
  <h2>Users</h2>
  <table>
   <xsl:for-each select="user">
   <tr><td>
    <xsl:value-of select="php:function ('MyClass::firstLetter',string(.))"/>
   </td></tr>
   </xsl:for-each>
  </table>
 </xsl:template>

 </xsl:stylesheet>

And the odd output (notice the xmlns:php="http://php.net/xsl"):

<!DOCTYPE html> 
<html> 
<head></head>
<body>
 <h2 xmlns:php="http://php.net/xsl">Users</h2>
 <table xmlns:php="http://php.net/xsl">
  <tr><td>b</td></tr>
  <tr><td>t</td></tr>
 </table>
</body>
</html>

Any ideas? I'm sure that I have all the imports I need and am using them correctly but if someone can show me what or where I'm going wrong, that would be great. Thanks in advance.

0

1 Answer 1

3
 <xsl:stylesheet version="1.0"    
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   
  xmlns:php="http://php.net/xsl"   
  xsl:extension-element-prefixes="php">

You need to add:

 exclude-result-prefixes="php" 

The exclude-result-prefixes attribute designates a list of prefixes and the namespaces bound to these prefixes are to be excluded (not copied) on literal result elements.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you soooo much, it was actually the 'extension-element-prefixes' element that was causing the issue. That fixed everything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.