2008-09-03

Namespaces in XSLT

XSLT tutorials and references usually pretend that namespaces don't exist. However, when trying to process an XML document (an XHTML document in this case) that declared a namespace, none of my element node references matched!

XSLT (rightly) distinguishes between element names with no namespace qualifier (which are processed as belonging in the "no namespace" namespace) and names with an explicit qualifier, which of course are treated as belonging to that namespace.

The solution is to

  • declare a prefix for the source document's namespace(s) in the XSLT document node, like (e.g.)

    <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:xhtml="http://www.w3.org/1999/xhtml">

  • use these prefixes in your patterns, like (e.g.)

    <xsl:template match="xhtml:p">



My thanks to Jeni Tennison for explaining it very clearly in this post.

No comments: