Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 15th, 2007, 11:39 PM
Registered User
 
Join Date: Oct 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Extra xmlns="" tag in output

I am using the transform below to convert an ADO persisted XML to ExcelXML. The conversion is done correctly except the following minor issue.

The output from <xslt:template match="z:row"> block at the bottom generates this --> <Row ss:Height="15" xmlns="">
instead of --> <Row ss:Height="15">

Where is this xmlns="" coming from? Any idea how to fix it


My Transform.
<?xml version="1.0" encoding="UTF-8"?>
<!--
This stylesheet is used to transform ADO recordset to Excel XML. By default, the
sheet converts the recordset to a complete Excel XML workbook
-->

<xslt:stylesheet xmlns:xslt="http://www.w3.org/1999/XSL/Transform"
                xmlns:rs="urn:schemas-microsoft-com:rowset"
                xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"
                xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
                xmlns:z="#RowsetSchema"
                xmlns:ms="urn:schemas-microsoft-com:xslt"
    xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
                xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
                version="1.0"
                exclude-result-prefixes="rs s dt z o x ss">

    <xslt:output method="xml" omit-xml-declaration="no" indent="yes" encoding="UTF-8"/>
  <xslt:template match="/">

    <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">
            <Styles>
                <Style ss:ID="Default" ss:Name="Normal">
                    <Alignment ss:Vertical="Bottom"/>
                    <Borders/>

                    <Interior/>
                    <NumberFormat/>
                    <Protection/>
                </Style>
                <Style ss:ID="AltItem">
                    <Alignment ss:Vertical="Top" ss:WrapText="1"/>
                    <Borders>
                        <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1" ss:Color="#C0C0C0"/>
                        <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1" ss:Color="#C0C0C0"/>
                        <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1" ss:Color="#C0C0C0"/>
                        <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1" ss:Color="#C0C0C0"/>
                    </Borders>

                    <Interior/>
                </Style>
                <Style ss:ID="Header">
                    <Alignment ss:Horizontal="Center" ss:Vertical="Top" ss:WrapText="1"/>
                    <Borders>
                        <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1" ss:Color="#C0C0C0"/>
                        <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1" ss:Color="#C0C0C0"/>
                        <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1" ss:Color="#C0C0C0"/>
                        <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1" ss:Color="#C0C0C0"/>
                    </Borders>

                    <Interior ss:Color="#4A3C8C" ss:Pattern="Solid"/>
                </Style>
                <Style ss:ID="Item">
                    <Alignment ss:Vertical="Top" ss:WrapText="1"/>
                    <Borders>
                        <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1" ss:Color="#C0C0C0"/>
                        <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1" ss:Color="#C0C0C0"/>
                        <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1" ss:Color="#C0C0C0"/>
                        <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1" ss:Color="#C0C0C0"/>
                    </Borders>

                    <Interior ss:Color="#CCCCFF" ss:Pattern="Solid"/>
                </Style>
            </Styles>
            <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
                <Author>Exported from TopTraders Web Application</Author>
                <Created>2003-04-22T13:43:53Z</Created>
                <Version>1.0</Version>
            </DocumentProperties>



      <Worksheet ss:Name="Sheet">
        <Table>

            <Row ss:Height="15">
              <xslt:for-each select="/xml/s:Schema/s:ElementType[@name='row']/s:AttributeType">
                <xslt:sort select="@name" />
                <Cell ss:StyleID="Header">
                  <Data ss:Type="String">
                    <xslt:value-of select="@name" />
                      </Data>
                </Cell>
              </xslt:for-each>
            </Row>


            <xslt:apply-templates select="/xml/rs:data/z:row">

            </xslt:apply-templates>
        </Table>
      </Worksheet>

        </Workbook>
  </xslt:template>


  <xslt:template match="z:row">
        <Row ss:Height="15">
      <xslt:variable name="current_row" select="." />
      <xslt:for-each select="/xml/s:Schema/s:ElementType[@name='row']/s:AttributeType">
        <xslt:sort select="@name" />
        <xslt:variable name="attrName" select="@name" />
                        <Cell ss:StyleID="Item">
                            <Data ss:Type="String">
            <xslt:choose>
              <xslt:when test="s:datatype/@dt:type='dateTime'">
                <xslt:value-of select="ms:format-date($current_row/@*[name()=$attrName],'MM/dd/yyyy')" />
              </xslt:when>
              <xslt:otherwise>
                <xslt:value-of select="$current_row/@*[name()=$attrName]" />
              </xslt:otherwise>
            </xslt:choose>
                            </Data>
                        </Cell>
      </xslt:for-each>
        </Row>
  </xslt:template>
</xslt:stylesheet>
 
Old October 16th, 2007, 01:47 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The following defines the default namespace for the Workbook element: <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet">

However the <Row> element in the next template has a blank default namespace, so when embedded inside th Workbook output element the default namespace is made explicit by adding the xmlns="".

Either add xmlns="urn:schemas-microsoft-com:office:spreadsheet" to the Row element, or move it up to the xsl:stylesheet element.



/- Sam Judson : Wrox Technical Editor -/
 
Old October 16th, 2007, 03:42 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If you generate elements with the right names (that is, namespace plus local name) then the namespace declarations will look after themselves. Your problem is that you wanted the Row element to be in a namespace, but you've generated it to be in no namespace. Its namespace is determined by the instruction used to create it - it won't automatically go in the same namespace as its parent element.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 16th, 2007, 11:23 AM
Registered User
 
Join Date: Oct 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks all for the help. I was under the assumption that the subsequent templates inherit the name space but I guess I was wrong.

Regards,

 
Old October 16th, 2007, 11:35 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The scope of namespace declarations is as defined in XML: it's based on the static lexical XML hierarchy of your stylesheet. It doesn't follow the dyamic call sequence. If you want to declare a namespace that applies to all your templates, put it on the xsl:stylesheet element.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
copying a tag to output based on a query anboss XSLT 6 July 17th, 2008 11:10 AM
How do I suppress the unwanted xmlns in output john7321 XSLT 2 November 24th, 2007 05:15 PM
HTML tag from C# or ASP.NET tag from javascript angshujit ASP.NET 2.0 Basics 3 February 16th, 2007 10:07 AM
How to force XSLT to output a close tag in XML? richcon XSLT 2 June 29th, 2006 02:39 AM
Trying to output a tag within a tag jaucourt XSLT 3 January 12th, 2005 11:57 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.