Search code examples
htmlxsltxml-namespacesxsl-foapache-fop

XSLT to generate xsl fo from HTML with varying namespace


I have a XSLT file which is used for html to xsl-fo conversion using fop engine. It has templates for HTML elements as shown below

<pre>

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:fo="http://www.w3.org/1999/XSL/Format"
                    version="2.0">

    <xsl:template match="html">
    //handle html element
    </xsl:template>

    <xsl:template match="head/title">
    //handle head/title elements
    </xsl:template>

    </xsl:stylesheet>

</pre>

I need to convert all kinds of HTML files provided as input to the processor. HTML files without namespace are processed without any issues. However, some HTML files have name space (<html xmlns="http://www.w3.org/1999/xhtml">) in which case the fop processor is throwing exceptions. What is the best way to handle this sort of cases? Can i create some template which ,based on the local-name(), call the correct template?


Solution

  • My preference in this kind of situation is to normalize the input before doing anything else, in a separate pass. This could be done with a template rule something like this:

    <xsl:template match="*">
      <xsl:element name="lower-case(local-name())">
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates/>
      </xsl:element>
    </xsl:template>