xslt-将任何xml转换为表

xslt-将任何xml转换为表,xml,excel,xslt,Xml,Excel,Xslt,我试图创建一个通用的XSLT文件来处理任何xml输入,我不想在XSLT中引用特定的子节点,因为xml可以是任何东西。我不希望xml的层次结构比10个子节点更深 示例xml: <Client> <LastName>Bill</LastName> <FirstName>Gates</FirstName> <MiddleName/> <Suffix/>

我试图创建一个通用的XSLT文件来处理任何xml输入,我不想在XSLT中引用特定的子节点,因为xml可以是任何东西。我不希望xml的层次结构比10个子节点更深

示例xml:

   <Client>
      <LastName>Bill</LastName>
      <FirstName>Gates</FirstName>
      <MiddleName/>
      <Suffix/>
      <DateOfBirth>30-May-1968</DateOfBirth>
      <PlaceOfBirth/>
      <SSN>n/a</SSN>
      <Gender>Male</Gender>
      <District>
        <City>SHELTON</City>
        <Mayor>wong</Mayor>
      </District>
      <State>WA</State>
      <Zip>96484</Zip>
   </Client>
   <Client>
      <LastName>Warron</LastName>
      <FirstName>Buffet</FirstName>
      <MiddleName>P</MiddleName>
      <Suffix/>
      <DateOfBirth>12-Aug-1957</DateOfBirth>
      <PlaceOfBirth>Mississippi</PlaceOfBirth>
      <SSN>n/a</SSN>
      <Gender>Male</Gender>
      <City>Missi</City>
      <State>KS</State>
      <Account>
        <Type>
        <Name>Cash</Name>
        <Currency>USD</Currency>
        <Country>USA</Country>
        </Type>
      </Account>
      <Zip>66096</Zip>
   </Client>

如果其中一个XML没有特定的标记,只需在表中显示一个空白单元格。标记作为列标题的顺序并不重要。目前xslt没有处理具有不同结构的xml。

如果您真的想将任何xml转换为表,那么您可以这样做,但它不是一个非常漂亮的表。(一些支持XML的关系数据库有一个名为“切碎”的操作,它试图做到这一点。)你当然不能通过使用一个可以处理普通XML的样式表,然后尝试对其进行泛化来实现这一点。

问题是,我需要在xslt中更改什么,以便它提供所需的输出。
<xsl:stylesheet version="1.0"
    xmlns="urn:schemas-microsoft-com:office:spreadsheet"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
 xmlns:msxsl="urn:schemas-microsoft-com:xslt"
 xmlns:user="urn:my-scripts"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" > 

<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">
    <xsl:apply-templates/>
  </Workbook>
</xsl:template>


<xsl:template match="/*">
  <Worksheet>
  <xsl:attribute name="ss:Name">
  <xsl:value-of select="local-name(/*/*)"/>
  </xsl:attribute>
    <Table x:FullColumns="1" x:FullRows="1">
      <Row>
        <xsl:for-each select="*[position() = 1]/*">
          <Cell><Data ss:Type="String">
          <xsl:value-of select="local-name()"/>
          </Data></Cell>
        </xsl:for-each>
      </Row>
      <xsl:apply-templates/>
    </Table>
  </Worksheet>
</xsl:template>


<xsl:template match="/*/*">
  <Row>
    <xsl:apply-templates/>
  </Row>
</xsl:template>


<xsl:template match="/*/*/*">
  <Cell><Data ss:Type="String">
    <xsl:value-of select="."/>
  </Data></Cell>
</xsl:template>


</xsl:stylesheet>
Private Sub CommandButton1_Click()
Dim sourceFile As String
Dim xlstFile As String
Dim exportFile As String
Dim filePath As String

filePath = Application.ThisWorkbook.path

sourceFile = filePath & "\client.xml"
xlstFile = filePath & "\trans.xml"
exportFile = filePath & "\export1.xls"

Transform sourceFile, xlstFile, exportFile
End Sub
Private Sub Transform(sourceFile, stylesheetFile, resultFile)

Dim source As New MSXML2.DOMDocument30
Dim stylesheet As New MSXML2.DOMDocument30
Dim result As New MSXML2.DOMDocument30

' Load data.
source.async = False
source.Load sourceFile

' Load style sheet.
stylesheet.async = False
stylesheet.Load stylesheetFile

If (source.parseError.ErrorCode <> 0) Then
   MsgBox ("Error loading source document: " & source.parseError.reason)
   Else
If (stylesheet.parseError.ErrorCode <> 0) Then
      MsgBox ("Error loading stylesheet document: " & stylesheet.parseError.reason)
   Else
      ' Do the transform.
      source.transformNodeToObject stylesheet, result
      result.Save resultFile
End If
End If

End Sub
LastName    FirstName   MiddleName  Suffix  DateOfBirth PlaceOfBirth    SSN Gender  District    State   Zip
Bill    Gates           30-May-1968     n/a Male     SHELTON wong    WA 96484
Warron  Buffet  P       12-Aug-1957 Mississippi n/a Male    Missi   KS  66096
Steev   Jobbs           19-Apr-1959 Cupertino   n/a Male    Cupertino   CA  96066