使用XSL进行XML到CSV的转换

时间:2014-04-29 10:38:14

标签: xml xslt csv

我一直致力于xml到csv转换,并使用java方法将XML文件转换为CSV文件。

我需要xsl文件才能获得exxted csv文件。

这是xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<Entity Type="MANAGER">
    <SubEntity>
       <SubEntityId>DEVICE</SubEntityId>
       <Table>
          <TableId>ReportTable</TableId>
          <CaptureTime>2014-03-20T13:00:00EDT</CaptureTime>
          <Labels>
             <Label>INSTANCE</Label>
             <Label>VALUE</Label>
             <Label>VALUE</Label>
             <Label>VALUE</Label>
          </Labels>
          <RowOfValues>
             <RowValue>
                <Value>Dummy</Value>
             </RowValue>
             <RowValue>
                <Value>0</Value>
             </RowValue>
             <RowValue>
                <Value>1</Value>
             </RowValue>
             <RowValue>
                <Value>0</Value>
             </RowValue>
          </RowOfValues>
          <RowOfValues>
             <RowValue>
                <Value>Temp</Value>
             </RowValue>
             <RowValue>
                <Value>3</Value>
             </RowValue>
             <RowValue>
                <Value>0</Value>
             </RowValue>
             <RowValue>
                <Value>0</Value>
             </RowValue>
          </RowOfValues>
          <RowOfValues>
             <RowValue>
                <Value>Random</Value>
             </RowValue>
             <RowValue>
                <Value>0</Value>
             </RowValue>
             <RowValue>
                <Value>0</Value>
             </RowValue>
             <RowValue>
                <Value>2</Value>
             </RowValue>
          </RowOfValues>
       </Table>
       <Table>
          <TableId>MatchTable</TableId>
          <CaptureTime>2014-03-20T13:00:00EDT</CaptureTime>
          <Labels>
             <Label>INSTANCE</Label>
             <Label>RESULT</Label>
             <Label>RESULT</Label>
          </Labels>
          <RowOfValues>
             <RowValue>
                <Value>Xyz</Value>
             </RowValue>
             <RowValue>
                <Value>0</Value>
             </RowValue>
             <RowValue>
                <Value>0</Value>
             </RowValue>
          </RowOfValues>
          <RowOfValues>
             <RowValue>
                <Value>Abc</Value>
             </RowValue>
             <RowValue>
                <Value>0</Value>
             </RowValue>
             <RowValue>
                <Value>1</Value>
             </RowValue>
          </RowOfValues>
       </Table>
    </SubEntity>
        <SubEntity>
       <SubEntityId>DEVICE</SubEntityId>
       <Table>
          <TableId>ReportTable</TableId>
          <CaptureTime>2014-03-20T13:00:00EDT</CaptureTime>
          <Labels>
             <Label>INSTANCE</Label>
             <Label>VALUE</Label>
             <Label>VALUE</Label>
             <Label>VALUE</Label>
          </Labels>
          <RowOfValues>
             <RowValue>
                <Value>Dummy</Value>
             </RowValue>
             <RowValue>
                <Value>0</Value>
             </RowValue>
             <RowValue>
                <Value>1</Value>
             </RowValue>
             <RowValue>
                <Value>0</Value>
             </RowValue>
          </RowOfValues>
          <RowOfValues>
             <RowValue>
                <Value>Temp</Value>
             </RowValue>
             <RowValue>
                <Value>3</Value>
             </RowValue>
             <RowValue>
                <Value>0</Value>
             </RowValue>
             <RowValue>
                <Value>0</Value>
             </RowValue>
          </RowOfValues>
          <RowOfValues>
             <RowValue>
                <Value>Random</Value>
             </RowValue>
             <RowValue>
                <Value>0</Value>
             </RowValue>
             <RowValue>
                <Value>0</Value>
             </RowValue>
             <RowValue>
                <Value>2</Value>
             </RowValue>
          </RowOfValues>
       </Table>
       <Table>
          <TableId>MatchTable</TableId>
          <CaptureTime>2014-03-20T13:00:00EDT</CaptureTime>
          <Labels>
             <Label>INSTANCE</Label>
             <Label>RESULT</Label>
             <Label>RESULT</Label>
          </Labels>
          <RowOfValues>
             <RowValue>
                <Value>Xyz</Value>
             </RowValue>
             <RowValue>
                <Value>0</Value>
             </RowValue>
             <RowValue>
                <Value>0</Value>
             </RowValue>
          </RowOfValues>
          <RowOfValues>
             <RowValue>
                <Value>Abc</Value>
             </RowValue>
             <RowValue>
                <Value>0</Value>
             </RowValue>
             <RowValue>
                <Value>1</Value>
             </RowValue>
          </RowOfValues>
       </Table>
    </SubEntity>    
</Entity>

以下是预期的csv文件:

#STARTTIME, STOPTIME, SubEntityId, ReportTable, INSTANCE, VALUE, VALUE, VALUE
2014-03-20T13:00:00EDT, 2014-03-20T13:00:00EDT DEVICE, ReportTable, Dummy, 0, 1, 0 
2014-03-20T13:00:00EDT, 2014-03-20T13:00:00EDT DEVICE, ReportTable, Temp, 3, 0, 0 
2014-03-20T13:00:00EDT, 2014-03-20T13:00:00EDT DEVICE, ReportTable, Random, 0, 0, 2

#STARTTIME, STOPTIME, SubEntityId, MatchTable, INSTANCE, RESULT, RESULT
2014-03-20T13:00:00EDT, 2014-03-20T13:00:00EDT DEVICE, MatchTable, Xyz, 0, 0 
2014-03-20T13:00:00EDT, 2014-03-20T13:00:00EDT DEVICE, MatchTable, Abc, 0, 1


#STARTTIME, STOPTIME, SubEntityId, ReportTable, INSTANCE, VALUE, VALUE, VALUE
2014-03-20T13:00:00EDT, 2014-03-20T13:00:00EDT DEVICE, ReportTable, Dummy, 0, 1, 0 
2014-03-20T13:00:00EDT, 2014-03-20T13:00:00EDT DEVICE, ReportTable, Temp, 3, 0, 0 
2014-03-20T13:00:00EDT, 2014-03-20T13:00:00EDT DEVICE, ReportTable, Random, 0, 0, 2

#STARTTIME, STOPTIME, SubEntityId, MatchTable, INSTANCE, RESULT, RESULT
2014-03-20T13:00:00EDT, 2014-03-20T13:00:00EDT DEVICE, MatchTable, Xyz, 0, 0 
2014-03-20T13:00:00EDT, 2014-03-20T13:00:00EDT DEVICE, MatchTable, Abc, 0, 1

这是我的xsl:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
         xmlns:fo="http://www.w3.org/1999/XSL/Format" >
   <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>

   <xsl:template match="/">
        <xsl:for-each select="//SubEntity[1]/Table">    
           <xsl:for-each select="//SubEntity[1]/Table[1]/RowOfValues[1]/RowValue">
              <xsl:variable name="pos" select="position()"/>
              <xsl:value-of select="normalize-space(.)"/>
              <xsl:call-template name="ScrapeColumns">
                 <xsl:with-param name="pos" select="$pos"/>
              </xsl:call-template>
              <xsl:text>&#xA;</xsl:text>
           </xsl:for-each>
        </xsl:for-each>     
   </xsl:template>

   <xsl:template name="ScrapeColumns">
      <xsl:param name="pos"></xsl:param>
      <xsl:for-each select="//SubEntity[1]/Table[1]/RowOfValues[position() > 1]//RowValue[position()=$pos]">
         <xsl:value-of select="concat(', ', normalize-space(.))"/>
      </xsl:for-each>   
   </xsl:template>

   <xsl:template name="ScrapeTables">
      <xsl:param name="pos"></xsl:param>
      <xsl:for-each select="//SubEntity[1]/Table[position() > 1]/RowOfValues[position() > 1]//RowValue[position()=$pos]">
      </xsl:for-each>   
   </xsl:template>

除了我的问题,如果我有超过2个SubEntity,我该如何解决?

我将非常感谢所有帮助。

1 个答案:

答案 0 :(得分:0)

猜测你想要的东西:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="/">
    <xsl:for-each select="SubEntity/Table">
        <!-- LABEL ROW -->
        <xsl:text>#STARTTIME,STOPTIME,SubEntityId,</xsl:text>
        <xsl:value-of select="TableId"/>
        <xsl:text>,</xsl:text>
        <xsl:for-each select="Labels/Label">
            <xsl:value-of select="."/>
            <xsl:if test="position()!=last()">
                <xsl:text>,</xsl:text>
            </xsl:if>
        </xsl:for-each>
        <xsl:text>&#10;</xsl:text>
        <!-- DATA ROWS -->
        <xsl:for-each select="RowOfValues">
            <xsl:value-of select="../CaptureTime"/>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="../CaptureTime"/>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="../../SubEntityId"/>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="../TableId"/>
            <xsl:text>,</xsl:text>
            <xsl:for-each select="RowValue/Value">
                <xsl:value-of select="."/>
                <xsl:if test="position()!=last()">
                    <xsl:text>,</xsl:text>
                </xsl:if>
            </xsl:for-each>     
            <xsl:if test="position()!=last()">
                <xsl:text>&#10;</xsl:text>
            </xsl:if>
        </xsl:for-each>
        <xsl:if test="position()!=last()">
            <xsl:text>&#10;&#10;</xsl:text>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

应用于更正的输入文档时:

<?xml version="1.0" encoding="UTF-8"?>
<SubEntity>
   <SubEntityId>DEVICE</SubEntityId>
   <Table>
      <TableId>ReportTable</TableId>
      <CaptureTime>2014-03-20T13:00:00EDT</CaptureTime>
      <Labels>
         <Label>INSTANCE</Label>
         <Label>VALUE</Label>
         <Label>VALUE</Label>
         <Label>VALUE</Label>
      </Labels>
      <RowOfValues>
         <RowValue>
            <Value>Dummy</Value>
         </RowValue>
         <RowValue>
            <Value>0</Value>
         </RowValue>
         <RowValue>
            <Value>1</Value>
         </RowValue>
         <RowValue>
            <Value>0</Value>
         </RowValue>
      </RowOfValues>
      <RowOfValues>
         <RowValue>
            <Value>Temp</Value>
         </RowValue>
         <RowValue>
            <Value>3</Value>
         </RowValue>
         <RowValue>
            <Value>0</Value>
         </RowValue>
         <RowValue>
            <Value>0</Value>
         </RowValue>
      </RowOfValues>
      <RowOfValues>
         <RowValue>
            <Value>Random</Value>
         </RowValue>
         <RowValue>
            <Value>0</Value>
         </RowValue>
         <RowValue>
            <Value>0</Value>
         </RowValue>
         <RowValue>
            <Value>2</Value>
         </RowValue>
      </RowOfValues>
   </Table>
   <Table>
      <TableId>MatchTable</TableId>
      <CaptureTime>2014-03-20T13:00:00EDT</CaptureTime>
      <Labels>
         <Label>INSTANCE</Label>
         <Label>RESULT</Label>
         <Label>RESULT</Label>
      </Labels>
      <RowOfValues>
         <RowValue>
            <Value>Xyz</Value>
         </RowValue>
         <RowValue>
            <Value>0</Value>
         </RowValue>
         <RowValue>
            <Value>0</Value>
         </RowValue>
      </RowOfValues>
      <RowOfValues>
         <RowValue>
            <Value>Abc</Value>
         </RowValue>
         <RowValue>
            <Value>0</Value>
         </RowValue>
         <RowValue>
            <Value>1</Value>
         </RowValue>
      </RowOfValues>
   </Table>
</SubEntity>

获得以下结果:

#STARTTIME,STOPTIME,SubEntityId,ReportTable,INSTANCE,VALUE,VALUE,VALUE
2014-03-20T13:00:00EDT,2014-03-20T13:00:00EDT,DEVICE,ReportTable,Dummy,0,1,0
2014-03-20T13:00:00EDT,2014-03-20T13:00:00EDT,DEVICE,ReportTable,Temp,3,0,0
2014-03-20T13:00:00EDT,2014-03-20T13:00:00EDT,DEVICE,ReportTable,Random,0,0,2

#STARTTIME,STOPTIME,SubEntityId,MatchTable,INSTANCE,RESULT,RESULT
2014-03-20T13:00:00EDT,2014-03-20T13:00:00EDT,DEVICE,MatchTable,Xyz,0,0
2014-03-20T13:00:00EDT,2014-03-20T13:00:00EDT,DEVICE,MatchTable,Abc,0,1

  

除了我的问题,如果我有超过2个SubEntity,我怎么能   解决它?

这个问题不明确。


修改

根据您编辑的问题,请更改此信息:

<xsl:for-each select="SubEntity/Table">

为:

<xsl:for-each select="Entity/SubEntity/Table">