0

多亏了 Will,我在生成自己的 XSLT 方面取得了一些进展,但是这个 XML 让我完全陷入困境,并且与我使用过的任何东西都不一样。由于它使用 MS Office 样式表,它会在 Excel 中打开,但遗憾的是我正在开发一个自动化流程。

任何关于我如何实现这一目标的线索都将受到欢迎。

转换原始 XML 数据:

<?xml version="1.0" encoding="UTF-8"?>
<ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<ss:Styles/>
<ss:Worksheet ss:Name="Information">
<ss:Table>
<ss:Row>
<ss:Cell>
<ss:Data ss:Type="String">Name</ss:Data>
</ss:Cell>
<ss:Cell>
<ss:Data ss:Type="String">Age</ss:Data>
</ss:Cell>
<ss:Cell>
<ss:Data ss:Type="String">Unit</ss:Data>
</ss:Cell>
</ss:Row>
<ss:Row>
<ss:Cell>
<ss:Data ss:Type="String">Sally</ss:Data>
</ss:Cell>
<ss:Cell>
<ss:Data ss:Type="String">29</ss:Data>
</ss:Cell>
<ss:Cell>
<ss:Data ss:Type="String">Greenford</ss:Data>
</ss:Cell>
</ss:Row>
<ss:Row>
<ss:Cell>
<ss:Data ss:Type="String">Dave</ss:Data>
</ss:Cell>
<ss:Cell>
<ss:Data ss:Type="String">45</ss:Data>
</ss:Cell>
<ss:Cell>
<ss:Data ss:Type="String">Paddington</ss:Data>
</ss:Cell>
</ss:Row>
</ss:Table>
</ss:Worksheet>
</ss:Workbook>

进入这个:

<?xml version="1.0" encoding="ISO-8859-1"?>
<Report Title="Location">
<Record>
<Name>Sally</Name>
<Age>29</Age>
<Unit>Greenford</Unit>
</Record>
<Record>
<Name>Dave</Name>
<Age>45</Age>
<Unit>Paddington</Unit>
</Record>
</Report>

谢谢。

4

1 回答 1

0

以下样式表将为您提供所需的输出 XML。

<xsl:stylesheet version="1.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" encoding="ISO-8859-1"/>
  <xsl:strip-space elements="*"/>

  <!-- Match the table, output the root node and select the rows
       excluding the row that contains the header. -->
  <xsl:template match="ss:Workbook/ss:Worksheet/ss:Table">
    <xsl:text>&#xa;</xsl:text>
    <Report Title="Location">
      <xsl:text>&#xa;</xsl:text>
      <xsl:apply-templates select="ss:Row[not(position()=1)]"/>
    </Report>
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>

  <!-- Now we're at a row. Match the data in each cell. -->
  <xsl:template match="ss:Row">
    <Record>
      <xsl:text>&#xa;</xsl:text>
      <xsl:apply-templates select="ss:Cell/ss:Data"/>
    </Record>
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>

  <!-- Output an element for each piece of data. -->
  <xsl:template match="ss:Data">
    <!-- Find which column we are at. -->
    <xsl:variable name="column" select="count(../preceding-sibling::ss:Cell) + 1"/>
    <!-- The name of the element comes from the corresponding column 
     in the header (the first row). -->
    <xsl:element name="{//ss:Workbook/ss:Worksheet/ss:Table/ss:Row[1]/ss:Cell[$column]/ss:Data}">
      <xsl:value-of select="."/>
    </xsl:element>
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>

</xsl:stylesheet>
于 2013-08-13T20:46:25.357 回答