Re: [xsl] FW: Problem with namespaces

Subject: Re: [xsl] FW: Problem with namespaces
From: "Jay Bryant" <jay@xxxxxxxxxxxx>
Date: Fri, 5 Aug 2005 12:53:42 -0500
Sorry to reply to my own message. I want to clarify the issue, lest someone
reading the archives get the wrong impression.

In a private message, Curtis pointed out that he had mapped the default
namespace to "ss". I had missed that, as I wasn't looking closely enough at
the namespaces. So the issue was knowing to use the "ss" namespace in the
XSL file to handle the default namespace in the source file. Here is the
corrected stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:o="urn:schemas-microsoft-com:office:office"
  xmlns:x="urn:schemas-microsoft-com:office:excel"
  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
  exclude-result-prefixes="o x ss"
>

  <xsl:output method="xml" indent="yes" encoding="UTF-8"/>

  <xsl:template match="ss:Workbook/ss:Worksheet/ss:Table/ss:Row[1]"/>

  <xsl:template match="ss:Workbook/ss:Worksheet/ss:Table/ss:Row[position()
&gt; 1]">
    <Inventory>
      <xsl:for-each select="ss:Cell">
        <xsl:choose>
          <xsl:when test="position() = 1">
            <Year><xsl:value-of select="ss:Data"/></Year>
          </xsl:when>
          <xsl:when test="position() = 2">
            <Make><xsl:value-of select="ss:Data"/></Make>
          </xsl:when>
          <xsl:when test="position() = 3">
            <Model><xsl:value-of select="ss:Data"/></Model>
          </xsl:when>
        </xsl:choose>
      </xsl:for-each>
    </Inventory>
  </xsl:template>

</xsl:stylesheet>

Thanks for your patience.

Jay Bryant
Bryant Communication Services

----- Original Message ----- 
From: <JBryant@xxxxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Thursday, August 04, 2005 2:27 PM
Subject: Re: [xsl] FW: Problem with namespaces


> Hi, Curtis,
>
> Your issue arises because the XML file has a default namespace:
>
> xmlns="urn:schemas-microsoft-com:office:spreadsheet"
>
> You need to handle that in your XSL file, thus:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
>   xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>   xmlns:ms="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"
>   exclude-result-prefixes="ms o x ss"
> >
>
>   <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
>
>   <xsl:template match="ms:Workbook/ms:Worksheet/ms:Table/ms:Row[1]"/>
>
>   <xsl:template match="ms:Workbook/ms:Worksheet/ms:Table/ms:Row[position()
> &gt; 1]">
>     <Inventory>
>       <xsl:for-each select="ms:Cell">
>         <xsl:choose>
>           <xsl:when test="position() = 1">
>             <Year><xsl:value-of select="ms:Data"/></Year>
>           </xsl:when>
>           <xsl:when test="position() = 2">
>             <Make><xsl:value-of select="ms:Data"/></Make>
>           </xsl:when>
>           <xsl:when test="position() = 3">
>             <Model><xsl:value-of select="ms:Data"/></Model>
>           </xsl:when>
>         </xsl:choose>
>       </xsl:for-each>
>     </Inventory>
>   </xsl:template>
>
> </xsl:stylesheet>
>
> As you can see, I greatly simplified your file, but I bet you can make
> sense of it.
>
> In addition to the simplifications, the specific changes that matter to
> this issue are:
>
> * I gave the default namespace an identifier (ms).
> * I used that identifier in all the matches and selects.
>
> The output (with Xalan Java 2.4.1, since you need XSLT 1.0) is:
>
> <Inventory>
>   <Year>2000</Year>
>   <Make>Buick</Make>
>   <Model>Park Ave</Model>
> </Inventory>
> <Inventory>
>   <Year>1998</Year>
>   <Make>Cadillac</Make>
>   <Model>Deville</Model>
> </Inventory>
> <Inventory>
>   <Year>1995</Year>
>   <Make>Cadillac</Make>
>   <Model>Deville</Model>
> </Inventory>
>
> (with a little hand indenting for readiblity)
>
> Saxon 8.5 produces the same results (indentation aside).
>
> Let us know if you need more help. Feel free to use me as a proxy for
> posting if need be.
>
> Jay Bryant
> Bryant Communication Services
> (presently consulting at Synergistic Solution Technologies)

Current Thread