简体   繁体   English

转换MS Office XML

[英]Transforming MS Office XML

Thanks to Will I've made some progress with generating my own XSLT, however this XML has me completely stuck and resembles nothing I've worked with. 多亏了Will的帮助,我在生成自己的XSLT方面取得了一些进展,但是这种XML使我完全陷入困境,与我使用过的任何东西都不相似。 Since it uses the MS Office stylesheet it opens up in Excel but sadly I'm working on an automated process. 由于它使用MS Office样式表,因此可以在Excel中打开,但是很遗憾,我正在从事自动化流程。

Any clues on how I can achieve this will be welcome. 关于如何实现此目标的任何线索都将受到欢迎。

Transform raw XML data: 转换原始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>

Into this: 变成这个:

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

Thanks. 谢谢。

The following stylesheet will give you the desired output XML. 以下样式表将为您提供所需的输出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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM