我有一个excel如下,第5行有标题数据。
编辑:
输入excel也可能如下所示。数据可能出现在任何列中。必须使用行标题Ad Name,UID和Status来识别数据。它不会改变。
然后保存为工作簿xml如下
<?xml version="1.0"?>
<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>Jefferson D</Author>
<LastAuthor>Jefferson D</LastAuthor>
<Created>2015-10-29T17:10:31Z</Created>
<LastSaved>2015-10-29T17:15:02Z</LastSaved>
<Company>*CL</Company>
<Version>12.0</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<AllowPNG/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>22060</WindowHeight>
<WindowWidth>34400</WindowWidth>
<WindowTopX>-20</WindowTopX>
<WindowTopY>-20</WindowTopY>
<Date1904/>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Verdana"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s16">
<Font ss:FontName="Verdana" ss:Bold="1"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="3" ss:ExpandedRowCount="10" x:FullColumns="1"
x:FullRows="1">
<Column ss:AutoFitWidth="0" ss:Width="176.0"/>
<Column ss:AutoFitWidth="0" ss:Width="141.0"/>
<Column ss:AutoFitWidth="0" ss:Width="152.0"/>
<Row>
<Cell ss:Index="2" ss:StyleID="s16"><Data ss:Type="String">Ad Report</Data></Cell>
</Row>
<Row ss:Index="3">
<Cell><Data ss:Type="String">IssueNo: 1</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">IssueName: XXX</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">Issue Date: YYY</Data></Cell>
</Row>
<Row ss:StyleID="s16">
<Cell><Data ss:Type="String">Ad Name</Data></Cell>
<Cell><Data ss:Type="String">UID</Data></Cell>
<Cell><Data ss:Type="String">Status</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">WWW</Data></Cell>
<Cell><Data ss:Type="String">0A1</Data></Cell>
<Cell><Data ss:Type="String">active</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">XXX</Data></Cell>
<Cell><Data ss:Type="String">1B2</Data></Cell>
<Cell><Data ss:Type="String">active</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">YYY</Data></Cell>
<Cell><Data ss:Type="String">2C3</Data></Cell>
<Cell><Data ss:Type="String">inactive</Data></Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<Print>
<ValidPrinterInfo/>
<PaperSizeIndex>10</PaperSizeIndex>
<HorizontalResolution>-4</HorizontalResolution>
<VerticalResolution>-4</VerticalResolution>
</Print>
<ShowPageLayoutZoom/>
<PageLayoutZoom>100</PageLayoutZoom>
<Selected/>
<Panes>
<Pane>
<Number>3</Number>
<ActiveRow>13</ActiveRow>
<ActiveCol>2</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
我想使用 XSLT2.0 从Excel xml文件中提取一些数据,并想要创建一个新的xml,如下所示
<adverts>
<advert>
<advertName>WWW</advertName>
<advertNumber>0A1</advertNumber>
<advertStatus>active<advertStatus>
</advert>
<advert>
<advertName>XXX</advertName>
<advertNumber>1B2</advertNumber>
<advertStatus>active<advertStatus>
</advert>
<advert>
<advertName>YYY</advertName>
<advertNumber>2C3</advertNumber>
<advertStatus>inactive<advertStatus>
</advert>
</adverts>
我非常困惑,因为这是我第一次处理工作簿XML。任何指导链接也很明显。
答案 0 :(得分:1)
在回复澄清时编辑:
数据可能出现在任何列中。必须使用确定数据 行标题广告名称,UID和状态。
以这种方式试试;
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
exclude-result-prefixes="ss">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="rows" select="/ss:Workbook/ss:Worksheet/ss:Table/ss:Row" />
<xsl:variable name="header-row" select="$rows[ss:Cell/ss:Data='Ad Name'][1]" />
<xsl:variable name="header-row-num" select="index-of($rows, $header-row)" />
<xsl:variable name="header-row-cells" select="$header-row/ss:Cell" />
<xsl:variable name="name-col-num" select="index-of($header-row-cells, $header-row-cells[ss:Data='Ad Name'][1])" />
<xsl:variable name="number-col-num" select="index-of($header-row-cells, $header-row-cells[ss:Data='UID'][1])" />
<xsl:variable name="status-col-num" select="index-of($header-row-cells, $header-row-cells[ss:Data='Stattus'][1])" />
<xsl:template match="/ss:Workbook">
<adverts>
<xsl:apply-templates select="ss:Worksheet/ss:Table/ss:Row[position() gt $header-row-num]"/>
</adverts>
</xsl:template>
<xsl:template match="ss:Row">
<advert>
<advertName>
<xsl:value-of select="ss:Cell[$name-col-num]/ss:Data"/>
</advertName>
<advertNumber>
<xsl:value-of select="ss:Cell[$number-col-num]/ss:Data"/>
</advertNumber>
<advertStatus>
<xsl:value-of select="ss:Cell[$status-col-num]/ss:Data"/>
</advertStatus>
</advert>
</xsl:template>
</xsl:stylesheet>
应用于XML输入示例,结果为:
<?xml version="1.0" encoding="UTF-8"?>
<adverts>
<advert>
<advertName>WWW</advertName>
<advertNumber>0A1</advertNumber>
<advertStatus>active</advertStatus>
</advert>
<advert>
<advertName>XXX</advertName>
<advertNumber>1B2</advertNumber>
<advertStatus>active</advertStatus>
</advert>
<advert>
<advertName>YYY</advertName>
<advertNumber>2C3</advertNumber>
<advertStatus>inactive</advertStatus>
</advert>
</adverts>
注意:
我有一个excel如下,其中第6行有标题数据。
与您的说法相反,并在屏幕截图中显示标题行 在您的XML中实际上是第5行,而不是第6行。样式表 上面通过包含的单元格的存在来标识标题行 “广告名称”。如果您事先知道该号码,则可以简化 直接使用该数字的样式表;
在XML中,列名称为“Stattus”而不是“Status”。因此,上面的样式表会查找“Stattus”,以便在处理XML示例时返回预期结果。
< / LI> 醇>