hung up on default namespaces



In my current project I'm dealing with transforming XML data using XSLT. The source XML document has it's default namespace explicitly defined, example:

<beach xmlns="http://www.dirtypotato.com/beach">
  <breezy name="Vikki" dpstatus="nondp" />
  <breezy name="Emily" dpstatus="birddog">
    <friend name="Jackie" takedownfactor="positive" />
  </breezy>
</beach>

My XSLT was defined as:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/beach">
    <xsl:apply-templates select="breezy" />
  </xsl:template>
</xsl:stylesheet>

When applying the stylesheet I received nothing in the output. This drove me crazy for a little while, and I knew that I could solve it with a nasty block of C# code that would add the appropriate namespaces, but I found the appropriate solution to the problem. Declare another prefix with the namespace from the source XML document. So now my XSLT looks like this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:earl="http://www.dirtypotato.com/beach">
  <xsl:template match="/earl:beach">
    <xsl:apply-templates select="breezy" />
  </xsl:template>
</xsl:stylesheet>

Any XPath statements used to select something from the root need the earl: prefix.

Here is an article that helped me out: http://www.xml.com/pub/a/2001/05/02/trxml.html



Posted by christopher andersson on August 11, 2005 11:12 AM

Comments