Search code examples
xsltxslt-1.0xml-namespacesphpdoc

Prevent xmlns="" attribute from being added to HTML elements generated by XSLT 1.0


I am in the process of creating a template for phpdoc 2 and I am trying to figure out how to prevent the attribute xmlns="http://www.w3.org/1999/xhtml" from being added to the root level elements of the generated HTML when the documentation is created from my template. My .xsl files in the template generate files containing html fragments, which is why the <html> tag is not the root level element. It appears phpdoc 2 uses XSLT v1.0, so that limits my options a bit. After doing some searching, the most common answer was to use the exclude-result-prefixes attribute on the <xsl:stylesheet> tag of each .xsl file, and set its value to either #all or #default, since you cannot just use xmlns as a namespace to exclude directly. But I tried that, setting the exclude-result-prefixes on every .xsl file in my template, and it did not work.

As requested, here's one of one of my .xsl files, api-doc.xsl:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns="http://www.w3.org/1999/xhtml" 
    exclude-result-prefixes="#all">

    <xsl:output indent="no" method="html"/>

    <xsl:include href="chrome.xsl"/>

    <xsl:include href="api-doc/property.xsl"/>
    <xsl:include href="api-doc/class.xsl"/>
    <xsl:include href="api-doc/constant.xsl"/>
    <xsl:include href="api-doc/function.xsl"/>
    <xsl:include href="api-doc/docblock.xsl"/>
    <xsl:include href="api-doc/file.xsl"/>

    <xsl:template name="content">
        <xsl:apply-templates select="/project/file[@path=$path]"/>
    </xsl:template>

</xsl:stylesheet>

This is a snippet of api-doc/file.xsl:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns="http://www.w3.org/1999/xhtml" 
    exclude-result-prefixes="#all">

    <xsl:output indent="no" method="html"/>

    <xsl:template match="file">
        <div class="text-content">
            <!-- ... -->
        </div>
    </xsl:template>

</xsl:stylesheet>

The element <div class="text-content"></div> is what has the xmlns="http://www.w3.org/1999/xhtml" attribute added to it.


Solution

  • Like Michael Kay said, if you don't want the div in the xhtml namespace, remove the default namespace (xmlns="http://www.w3.org/1999/xhtml") from your xsl:stylesheet.

    Otherwise you could build the element using xsl:element and give it an empty namespace:

        <xsl:element name="div" namespace="">
            <xsl:attribute name="class">text-content</xsl:attribute>
            <xsl:text>...</xsl:text>
        </xsl:element>