【发布时间】:2019-06-30 22:30:38
【问题描述】:
这是从 Excel Mac(Excel 2004 XML 格式)转换而来的 XML 文件:
Excel 文件有四个字段(字段 1 到 4),每个字段都有数据(Fieldn_Datam,n,m 范围从 1 到 4)
XML 输出是这样的:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<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>Passiflora Cui</Author>
<LastAuthor>Passiflora Cui</LastAuthor>
<Created>2019-06-30T21:49:41Z</Created>
<LastSaved>2019-06-30T21:50:54Z</LastSaved>
<Version>16.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<AllowPNG/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>16940</WindowHeight>
<WindowWidth>27640</WindowWidth>
<WindowTopX>5580</WindowTopX>
<WindowTopY>3560</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="12" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s62">
<Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="4" ss:ExpandedRowCount="5" x:FullColumns="1"
x:FullRows="1" ss:DefaultColumnWidth="65" ss:DefaultRowHeight="16">
<Column ss:AutoFitWidth="0" ss:Width="74"/>
<Row>
<Cell>
<Data ss:Type="String">Field1</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field2</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field3</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field4</Data>
</Cell>
</Row>
<Row>
<Cell>
<Data ss:Type="String">Field1_Data1</Data>
</Cell>
<Cell ss:StyleID="s62">
<Data ss:Type="String">Field2_Data1</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field3_Data1</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field4_Data1</Data>
</Cell>
</Row>
<Row>
<Cell>
<Data ss:Type="String">Field1_Data2</Data>
</Cell>
<Cell ss:StyleID="s62">
<Data ss:Type="String">Field2_Data2</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field3_Data2</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field4_Data2</Data>
</Cell>
</Row>
<Row>
<Cell>
<Data ss:Type="String">Field1_Data3</Data>
</Cell>
<Cell ss:StyleID="s62">
<Data ss:Type="String">Field2_Data3</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field3_Data3</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field4_Data3</Data>
</Cell>
</Row>
<Row>
<Cell>
<Data ss:Type="String">Field1_Data4</Data>
</Cell>
<Cell ss:StyleID="s62">
<Data ss:Type="String">Field2_Data4</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field3_Data4</Data>
</Cell>
<Cell>
<Data ss:Type="String">Field4_Data4</Data>
</Cell>
</Row>
</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>5</ActiveRow>
<ActiveCol>5</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
而我的 XSLT 文件是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version = "1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method = "xml" indent = "yes"/>
<xsl:template match = "Table">
<Result>
<xsl:for-each select="Row[position() > 1]">
<Test>
<Field1><xsl:value-of select = "Cell[1]/Data"/></Field1>
<Field2><xsl:value-of select = "Cell[2]/Data"/></Field2>
<Field3><xsl:value-of select = "Cell[3]/Data"/></Field3>
<Field4><xsl:value-of select = "Cell[4]/Data"/></Field4>
</Test>
</xsl:for-each>
</Result>
</xsl:template>
</xsl:stylesheet>
所需的结果文件如下所示:
<Result>
<Test>
<Field1>Field1_Data1</Field1>
<Field2>Field2_Data1</Field2>
<Field3>Field3_Data1</Field3>
<Field4>Field4_Data1</Field4>
</Test>
<Test>
<Field1>Field1_Data2</Field1>
<Field2>Field2_Data2</Field2>
<Field3>Field3_Data2</Field3>
<Field4>Field4_Data2</Field4>
</Test>
<Test>
<Field1>Field1_Data3</Field1>
<Field2>Field2_Data3</Field2>
<Field3>Field3_Data3</Field3>
<Field4>Field4_Data3</Field4>
</Test>
<Test>
<Field1>Field1_Data4</Field1>
<Field2>Field2_Data4</Field2>
<Field3>Field3_Data4</Field3>
<Field4>Field4_Data4</Field4>
</Test>
</Result>
但是,我的 XSLT 不起作用,它会输出所有字符串,就好像 <xsl:template> 没有被执行一样。我尝试了很多 XPath,例如 //Table、Workbook/Worksheet/Table,但都没有奏效!谁能告诉我我错在哪里?提前致谢!
【问题讨论】:
-
你绝对需要使用 xslt 吗?或者您可以使用 VBA 或其他编程语言吗?
-
因为我用的是Office for Mac,而VBA没有Windows强大,我也想试试用XSLT来解决这个问题。