xml - 在导入 Excel 之前对 XML 应用 XSLT

标签 xml excel xslt

我有一个应用于 XML 数据文件的 XSLT 模板。该模板生成一个 Excel 电子表格 XML 文件。

除了一个小问题外,一切都很好......

当我在 Excel 中打开电子表格时,它会将其视为 XML 导入并询问我是否应应用样式表。我希望它能自动应用与该文件关联的任何 XSLT 模板。

下面是一些示例代码....提前致谢....

XML 文档...

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="spreadstyle.xsl"?>

<ResponseBody>
    <ActivityDetails>
        <ActivityDetail>
            <TranCodeDesc>LateChargeAssessment</TranCodeDesc>
        </ActivityDetail>
    </ActivityDetails>
</ResponseBody>

XSLT 模板...

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<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">

     <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
      <Author>tbarbedo</Author>
      <Created>2009-05-29T18:21:48Z</Created>
      <Version>12.00</Version>
     </DocumentProperties>
     <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
      <WindowHeight>8895</WindowHeight>
      <WindowWidth>18015</WindowWidth>
      <WindowTopX>0</WindowTopX>
      <WindowTopY>105</WindowTopY>
      <ProtectStructure>False</ProtectStructure>
      <ProtectWindows>False</ProtectWindows>
     </ExcelWorkbook>
     <Styles>
      <Style ss:ID="Default" ss:Name="Normal">
       <Alignment ss:Vertical="Bottom"/>
       <Borders/>
       <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
       <Interior/>
       <NumberFormat/>
       <Protection/>
      </Style>
     </Styles>
     <Worksheet ss:Name="Sheet1">
      <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="5000" x:FullColumns="1"
       x:FullRows="1" ss:DefaultRowHeight="15">
            <Row><Cell><Data ss:Type="String">Transaction Code Descriptions (MAGIC) </Data></Cell></Row>
            <xsl:for-each select="ResponseBody/ActivityDetails/ActivityDetail">     
                <Row> 
                    <Cell>
                        <Data ss:Type="String">
                            <xsl:value-of select="TranCodeDesc" />
                        </Data>
                    </Cell>

                </Row>
            </xsl:for-each>

      </Table>
      <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
       <PageSetup>
        <Header x:Margin="0.3"/>
        <Footer x:Margin="0.3"/>
        <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
       </PageSetup>
       <Selected/>
       <Panes>
        <Pane>
         <Number>3</Number>
         <ActiveRow>1</ActiveRow>
        </Pane>
       </Panes>
       <ProtectObjects>False</ProtectObjects>
       <ProtectScenarios>False</ProtectScenarios>
      </WorksheetOptions>
     </Worksheet>
    </Workbook>

</xsl:template>
</xsl:stylesheet>

最佳答案

根据我的理解,无法绕过显示的导入 XML... 对话框。这篇 MSDN 文章 ( Transform XML Files with XSLT When Importing into Microsoft Excel 2002 ) 中解释了原因:

XML is a markup language used for describing structured data (such as that in a worksheet) so that it can be read by a variety of applications. Designers can create customized XML elements, enabling the definition, transmission, validation, and interpretation of data between applications and between organizations. This versatility provides many opportunities for data interchange, but it also has at least one inherent challenge. By manipulating the elements of an XML document, designers can create any number of nested element dimensions within a document. These parent/child element relationships can lead to ambiguity in the two-dimensional row-and-column paradigm of an Excel worksheet.

To deal with this ambiguity, Excel forces a format to any XML data it receives, unless the data comes with a style sheet that predefines another format. This forced format is created by using a flattening algorithm to populate the rows and columns that comprise a worksheet. Yet while this format is effective in forcing multidimensional data into a two-dimensional format, it doesn't always present the data in the most optimal format for a human reader.

So how does Excel know when to use either the flattening algorithm or the XSLT style sheet? When Excel opens or imports an XML data file, it looks for an element inside the file that points to an XSLT style sheet. If that element is present, Excel displays a dialog box that prompts you to apply the style sheet or to open the file without a style sheet.

关于xml - 在导入 Excel 之前对 XML 应用 XSLT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/928129/

相关文章:

c# - 如何在 ASP.NET MVC 中将 XML 文件发送到客户端

xml - 可以在 XSD 中控制元素序列中的文本吗?

sql - 是否有等效的 excel 到 sql 查询的综述?

VBA比较两列并复制行

xml - 如何在 XSLT 中实现 if-else 语句?

xslt - 如何将元素插入到 xslt 中先前创建的元素中?

android - 如何将两种不同的样式应用于android中的一个元素?

xml - 在 XSLT 1.0 中使用带有分隔符的 concat()

Excel:使用带有 OR 函数的 MAXIFS

xml - 用于确定 XSLT 和 XML 数据源之间未使用的元素的脚本