Categories
World Wide Web

The default namespace problem in XSLT 1.0

(This blog is only for my reference. Non-XSLT users please dis-regard
this blog entry. XSLT users please suggest some alternatives/comments.)

There is a problem popularly called the default namespace problem in
XSLT 1.0. Supposedly this problem has been solved in the newer version
of XSLT (XSLT 2.0).

The problem is not discussed here, rather the solution is given below:

Consider an input XML file bound to a default namespace:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="new.xsl"?>
<catalog xmlns="http://buzypi.50webs.com/">
 <entry>1</entry>
 <entry>2</entry>
 <entry>3</entry>
</catalog>


Consider the XSLT style-sheet:

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="http://buzypi.50webs.com/"
>

<xsl:template match="/">
 <html>
 <body>
  <xsl:apply-templates select="my:catalog/my:entry" />
 </body>
 </html>
</xsl:template>

<xsl:template match="my:entry">
 <h1><xsl:value-of select="." /></h1>
</xsl:template>

</xsl:stylesheet>

The problem is thus solved by defining our own namespace and using it to refer to all default namespace elements.

There is another solution using local-name() function of XPath (Please refer to biglist link below for that solution).

XSLT 2.0 has something called default-xpath-namespace, which is used to solve the problem.

This thread on biglist deals with the default namespace problem.