从xsl问题生成excel

时间:2010-11-09 14:08:13

标签: xml xslt

我正在尝试从xsl生成excel文件,但是xslo会自动为模板节点的每个根元素生成xmlns属性: xsl:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<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"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
                exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>

  <xsl: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" >
 ...
      <xsl:call-template name="Styles"></xsl:call-template>
    ...
    </Workbook>
  </xsl:template>
  <xsl:template name ="Styles">
    <Styles >
      <Style ss:ID="Default" ss:Name="Normal">
        <Alignment ss:Vertical="Bottom"/>
        <Borders/>
        <Font/>
        <Interior/>
        <NumberFormat/>
        <Protection/>
      </Style>
      <Style ss:ID="m20452808">
        <Alignment ss:Vertical="Bottom" ss:WrapText="1"/>
        <Borders>
          <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"
           ss:Color="#000000"/>
          <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"
           ss:Color="#000000"/>
          <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"
           ss:Color="#000000"/>
        </Borders>
        <Interior ss:Color="#FFFF00" ss:Pattern="Solid"/>
      </Style>
  </Styles>
  </xsl:template>

输出:

<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">
  <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
    <LastAuthor>Harold</LastAuthor>
    <Created>2010-11-09T09:41:05Z</Created>
    <LastSaved>2010-11-09T09:41:05Z</LastSaved>
    <Version>11.5606</Version>
  </DocumentProperties>
  <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
    <WindowHeight>12780</WindowHeight>
    <WindowWidth>18795</WindowWidth>
    <WindowTopX>240</WindowTopX>
    <WindowTopY>315</WindowTopY>
    <ProtectStructure>False</ProtectStructure>
    <ProtectWindows>False</ProtectWindows>
  </ExcelWorkbook>
<Styles xmlns="">

问题:

  <Styles xmlns="">
.....

任何想法?????

1 个答案:

答案 0 :(得分:2)

  

问题:

     

<Styles xmlns="">

这是一个重置默认名称空间声明(在XML 1.1中,您可以重置前缀名称空间声明)。为什么?因为Workbook文字结果元素中声明的默认命名空间传播到样式表中的后代 ,但Styles不是样式表中的后代 它位于空名称空间URI下。

解决方案:声明样式表中公共祖先中所有文字结果元素的默认命名空间。好的做法是在xsl:stylesheet根元素中声明它。