template adds unexpected namespace

R

Rolf Kemper

Dear Experts,

I'm going to create an Excell spreadsheet xml. So far things work very
well. But in case I add <Row> elements by a recursive template call it
goes wrong.

See the attached XSLT and the result. Last 30 lines of the xslt may be
the most interesting ones. I'm processing it with XMLSPY. Regardless
of the xslt processor (ALTOVA / MSXML4 ) I get the same result.

Hence, I belive I have not done it right. Sorry for the long code, but
I do not really know where the problem might be.

Any help is welcome
Regards
Rolf


######################### XSLT ###################################

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:cdb="http://intra.etc.nec.de/cdb.html"
xmlns:eek:="urn:schemas-microsoft-com:eek:ffice:eek:ffice"
xmlns:x="urn:schemas-microsoft-com:eek:ffice:excel"
xmlns:ss="urn:schemas-microsoft-com:eek:ffice:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<xsl:eek:utput method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="max_hierarchy">
<xsl:for-each select="//xs:element">
<xsl:sort select="-count(ancestor::xs:element)"
data-type="number"/>
<xsl:if test="position()=1">
<xsl:value-of select="count(ancestor-or-self::xs:element)"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="items" select="9"/>
<xsl:variable name="FirstItemIndex" select="$max_hierarchy * 2 +
1"/>
<xsl:variable name="ExpandedColumnCount" select="$FirstItemIndex +
$items"/>
<xsl:variable name="Elements"
select="count(xs:schema/xs:element[@name='Chip']//xs:element)"/>
<!--header row counts -->
<xsl:variable name="ExpandedRowCount" select="$Elements + 1"/>
<!-- <xsl:variable name="header_items"
select="document('<items><i>A</i><i>B</i></items>','/items'"-->
<Workbook xmlns="urn:schemas-microsoft-com:eek:ffice:spreadsheet"
xmlns:eek:="urn:schemas-microsoft-com:eek:ffice:eek:ffice"
xmlns:x="urn:schemas-microsoft-com:eek:ffice:excel"
xmlns:ss="urn:schemas-microsoft-com:eek:ffice:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties
namespace="urn:schemas-microsoft-com:eek:ffice:eek:ffice">
<Author>CreateXLSXMLFromChipDBSchema</Author>
<LastAuthor>
<xsl:value-of select="//HISTORY//XSDHISTORY[last()]/@who"/>
</LastAuthor>
<Company>NEC Electronics (Europe)</Company>
</DocumentProperties>
<ExcelWorkbook namespace="urn:schemas-microsoft-com:eek:ffice:excel">
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>

snipped ..........

</Styles>
<Worksheet ss:Name="SchemaStructure">
<Table ss:ExpandedRowCount="{$ExpandedRowCount + 100}"
ss:ExpandedColumnCount="{$ExpandedColumnCount}" x:FullColumns="0"
x:FullRows="1">
<!-- column widths -->
<Column ss:Index="{$FirstItemIndex + 0}" ss:Width="20"/>
snipped ....
<Column ss:Index="{$FirstItemIndex + 8}" ss:Width="100"/>
<!-- header line -->
<Row>
<Cell ss:MergeAccross="{$FirstItemIndex}"
ss:StyleID="header_element">
<Data ss:Type="String">Hierarchy Element</Data>
</Cell>
<Cell ss:Index="{$FirstItemIndex + 0}" ss:StyleID="header_item">
<Data ss:Type="String">MinBound</Data>
</Cell>

snipped ......

<Cell ss:Index="{$FirstItemIndex + 8}" ss:StyleID="header_item">
<Data ss:Type="String">Example</Data>
</Cell>
</Row>
<Row><!-- this works well -->
<Cell ss:StyleID="TreeLine">
<Data ss:Type="String">├</Data>
</Cell>
</Row>
<!-- Rows inserted by the template get a null namespace with it
!!!1 -->
<xsl:apply-templates select="xs:schema/xs:element[@name='Chip']"
mode="e"/>
</Table>
</Worksheet>
</Workbook>
</xsl:template>

<xsl:template match="*" mode="e">
<!--<xsl:element name="Row"> the same behaviour as <Row> -->
<Row> <!-- here namespace xmlns="" is added as attribute WHY ??? -->
<xsl:for-each select="./ancestor::xs:element">
<!-- Cell gets no additional namspace with it!! -->
<Cell ss:StyleID="TreeLine">
<Data ss:Type="String">│</Data>
</Cell>
</xsl:for-each>
<Cell ss:StyleID="TreeLine">
<Data ss:Type="String">├</Data>
</Cell>
<Cell ss:StyleID="TreeLine">
<Data ss:Type="String">
<xsl:value-of select="@name"/>
</Data>
</Cell>
</Row>
<xsl:apply-templates
select="./xs:complexType/xs:sequence/xs:element" mode="e"/>
</xsl:template>
</xsl:stylesheet>


############## RESULT ##############################

<?xml version="1.0" encoding="UTF-8"?>
<Workbook xmlns="urn:schemas-microsoft-com:eek:ffice:spreadsheet"
xmlns:cdb="http://intra.etc.nec.de/cdb.html"
xmlns:html="http://www.w3.org/TR/REC-html40"
xmlns:eek:="urn:schemas-microsoft-com:eek:ffice:eek:ffice"
xmlns:ss="urn:schemas-microsoft-com:eek:ffice:spreadsheet"
xmlns:x="urn:schemas-microsoft-com:eek:ffice:excel"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<DocumentProperties
namespace="urn:schemas-microsoft-com:eek:ffice:eek:ffice">
<Author>CreateXLSXMLFromChipDBSchema</Author>
<LastAuthor>kemperr</LastAuthor>
<Company>NEC Electronics (Europe)</Company>
</DocumentProperties>
<ExcelWorkbook namespace="urn:schemas-microsoft-com:eek:ffice:excel">
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="header_element">
<Alignment ss:Horizontal="Left" ss:Vertical="Bottom"/>
<Interior ss:Color="silver" ss:pattern="Solid"/>
</Style>
<Style ss:ID="header_item">
<Alignment ss:Horizontal="Left" ss:Vertical="Bottom"/>
</Style>
<Style ss:ID="element">
<Alignment ss:Horizontal="Left" ss:Vertical="Bottom"/>
<Interior ss:Color="orange" ss:pattern="Solid"/>
</Style>
<Style ss:ID="leaf_element">
<Alignment ss:Horizontal="Left" ss:Vertical="Bottom"/>
<Interior ss:Color="blue" ss:pattern="Solid"/>
</Style>
<Style ss:ID="TreeLine">
<Alignment ss:Horizontal="Right" ss:Vertical="Bottom"/>
</Style>
</Styles>
<Worksheet ss:Name="SchemaStructure">
<Table ss:ExpandedRowCount="228" ss:ExpandedColumnCount="22"
x:FullColumns="0" x:FullRows="1">
<Column ss:Index="13" ss:Width="20"/>
<Column ss:Index="14" ss:Width="20"/>
<Column ss:Index="15" ss:Width="100"/>
<Column ss:Index="16" ss:Width="100"/>
<Column ss:Index="17" ss:Width="30"/>
<Column ss:Index="18" ss:Width="30"/>
<Column ss:Index="19" ss:Width="100"/>
<Column ss:Index="20" ss:Width="100"/>
<Column ss:Index="21" ss:Width="100"/>
<Row>
<Cell ss:MergeAccross="13" ss:StyleID="header_element">
<Data ss:Type="String">Hierarchy Element</Data>
</Cell>
<Cell ss:Index="13" ss:StyleID="header_item">
<Data ss:Type="String">MinBound</Data>
</Cell>
<Cell ss:Index="14" ss:StyleID="header_item">
<Data ss:Type="String">MaxBound</Data>
</Cell>
<Cell ss:Index="15" ss:StyleID="header_item">
<Data ss:Type="String">ToolTip</Data>
</Cell>
<Cell ss:Index="16" ss:StyleID="header_item">
<Data ss:Type="String">Help</Data>
</Cell>
<Cell ss:Index="17" ss:StyleID="header_item">
<Data ss:Type="String">HierarchyName</Data>
</Cell>
<Cell ss:Index="18" ss:StyleID="header_item">
<Data ss:Type="String">LeafChild</Data>
</Cell>
<Cell ss:Index="19" ss:StyleID="header_item">
<Data ss:Type="String">FilterRegEx</Data>
</Cell>
<Cell ss:Index="20" ss:StyleID="header_item">
<Data ss:Type="String">FilterFunc</Data>
</Cell>
<Cell ss:Index="21" ss:StyleID="header_item">
<Data ss:Type="String">Example</Data>
</Cell>
</Row>
<Row>
<Cell ss:StyleID="TreeLine">
<Data ss:Type="String">├</Data>
</Cell>
</Row>
<Row xmlns="">
<Cell ss:StyleID="TreeLine">
<Data ss:Type="String">├</Data>
</Cell>
<Cell ss:StyleID="TreeLine">
<Data ss:Type="String">Chip</Data>
</Cell>
</Row>
<Row xmlns="">
<Cell ss:StyleID="TreeLine">
<Data ss:Type="String">│</Data>
</Cell>
<Cell ss:StyleID="TreeLine">
<Data ss:Type="String">├</Data>
</Cell>
<Cell ss:StyleID="TreeLine">
<Data ss:Type="String">XMLHISTORY</Data>

snipped .....


##################### end #########################
 
D

David Carlisle

<Row> <!-- here namespace xmlns="" is added as attribute
WHY ??? -->

Because at that point in the stylesheet the default namespace is
no-namespace so
<Row>
constructs an element with local name Row and namespace URI ""

But in the result tree the parent element has a namespace of

xmlns="urn:schemas-microsoft-com:eek:ffice:spreadsheet"

so XSLT has to add xmlns="" to the start tag to keep Row in
no-namespace.

Solution, add

xmlns="urn:schemas-microsoft-com:eek:ffice:spreadsheet"
to the current <Row> tag in teh stylesheet or more simply just move all
the namespace declarations that you want in the result xsl:stylesheet
element so that they are in scopr for your whole stylesheet ie move them
from
here:
<Workbook xmlns="urn:schemas-microsoft-com:eek:ffice:spreadsheet"
xmlns:eek:="urn:schemas-microsoft-com:eek:ffice:eek:ffice"
xmlns:x="urn:schemas-microsoft-com:eek:ffice:excel"
xmlns:ss="urn:schemas-microsoft-com:eek:ffice:spreadsheet"


which means they are only in scope inside the template for "/".

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top