Apparently (see section 2.2.2.1 ‘Must Allow Matching on Default Namespace Without Explicit Prefix’) this is a common problem.

You are transforming a document:


<doc>
  <a>Hello</a>
</doc>

With a stylesheet:


<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:template match="doc">
    <foo>
      <xsl:apply-templates/>
    </foo>
  </xsl:template>

  <xsl:template match="a">
    <a>
      <xsl:apply-templates/>
    </a>
  </xsl:template>
</xsl:stylesheet>

When you add a namespace declaration to your document, your stylesheet stops working properly. It just outputs text!


<doc xmlns="http://somewhere/something">
  <a>Hello</a>
</doc>

The solution is to add the same namespace declaration to your XSLT, specifying a prefix, and then to use that prefix when matching:


<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ns="http://somewhere/something"
>
  <xsl:template match="ns:doc">
    <foo>
      <xsl:apply-templates/>
    </foo>
  </xsl:template>

  <xsl:template match="ns:a">
    <a>
      <xsl:apply-templates/>
    </a>
  </xsl:template>
</xsl:stylesheet>
No comments