XSLT and the default namespace

A colleague of mine who is relatively new to XSLT was getting bitten by an issue with a “hello world” stylesheet where the elements in the source document were all in a namespace and he wasn’t addressing or even including the namespace in the declarations of the XSLT. I found a great write-up that describes the issue at hand, but there is one point that Steve makes in that explanation which is rather subtle. When he says:

…because in XPath 1.0, an unqualified element in a match pattern only matches elements with no namespace URI at all.

All the info is there in that sentence, but it is, especially for someone fairly new to XML namespaces and XSLT, a pretty dense sentence.So, to be more explicit, when you include the namespace declaration in the XSLT you MUST use a namespace prefix. So, referring back to Steve’s post, the following would NOT work.

<xsl :stylesheet version="1.0" xmlns:xdl="http://www.w3.org/1999/XSL/Transform"
   xmlns="uri:something">
      ...    
     <xsl :template match="Foo/Bar[@baz='bop']">

The stylesheet is totally valid, but the xPath in the “match” attribute says to look for a “Foo” element with no namespace and a “Bar” element with no namespace.

Make sense?

One Comment

  1. Dave Pawson

    November 22, 2010

Share Your Thoughts