Re: [xsl] Default namespace matching

Subject: Re: [xsl] Default namespace matching
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 29 Dec 2006 13:33:07 -0500
Ed,

At 12:13 PM 12/29/2006, you wrote:
I think I must be misunderstanding something fundamental about
namespaces.  Is it not the case that if your source file has a default
namespace, in order to get the stylesheet to match, all you have to do
is include the same namespace in the declaration?

No, it's not.


The default namespace in a stylesheet will apply to unprefixed elements, but it doesn't apply to elements referred to in data content -- most particularly, in XPath expressions, which means "select" and "match" patterns.

But in your case, you already have the namespace bound in the stylesheet to the prefix "ss". So you could say:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
  xmlns:o="urn:schemas-microsoft-com:office:office"
  xmlns:x="urn:schemas-microsoft-com:office:excel"
  xmlns:html="http://www.w3.org/TR/REC-html40";>
                <!-- pull in structure  -->
        <xsl:template match="ss:Workbook">

Because the "urn:schemas-microsoft-com:office:spreadsheet" is declared as the default namespace in your source documents, "ss:Workbook" in the match pattern in the stylesheet will match the "Workbook" element in the input.

Please note that I've said match="ss:Workbook" where you have match="//Workbook". In a match pattern (though not elsewhere) "ss:Workbook" is the same as saying "//ss:Workbook", only better for all kinds of subtle reasons. If you don't understand the difference or why this is, this is a good sign that you're faking it with the language and need to study up on the XSLT processing model and How Templates Work. Otherwise you'll find it difficult to use XSLT for more than trivial applications.

(Doing the right thing is good. But knowing why is something altogether better -- partly because then you'll know when not to do it.)

Good luck,
Wendell



  My stylesheet works
fine if I delete the xmlns declaration on both files - can someone
please tell me how to make it work without any modification?  I've been
stuck on this for ages....any help appreciated!

Cheers,
   Ed

Please see below.

Here is my source file.

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40";>
 ....
</Workbook>

Here is part of my stylesheet.

<xsl:stylesheet version="1.0"
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:html="http://www.w3.org/TR/REC-html40";>
                <!-- pull in structure  -->
        <xsl:template match="//Workbook">
                <mapping>

                        <xsl:for-each select="Worksheet">
                                <xsl:apply-templates select="."/>
                        </xsl:for-each>
                </mapping>
        </xsl:template>

        <xsl:template match="Worksheet[@ss:Name='Zone']">
                        <xsl:for-each select="Table/Row">
                                <xsl:apply-templates
select="Cell[1]/Data[@ss:Type='String']" mode="zone"/>
                        </xsl:for-each>
        </xsl:template>
</xsl:stylesheet>


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread