【问题标题】:XML to CSV conversion using XSL使用 XSL 将 XML 转换为 CSV
【发布时间】:2014-04-29 10:38:14
【问题描述】:

我一直在研究 xml 到 csv 的转换,并使用 java 方法将 XML 文件转换为 CSV 文件。

我需要 xsl 文件以获取 exexted 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 个子实体,我该如何解决?

我将不胜感激。

【问题讨论】:

  • 请也发布您的 xsl 尝试?
  • 您的 XML 无效:您不能拥有像 &lt;Table Table_A&gt; 这样的标签。
  • 我刚刚分享了我的 xsl 文件

标签: xml xslt csv


【解决方案1】:

你想要这样的东西:

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 个子实体,我怎么能 修好吗?

这个问题不清楚。


编辑

针对您编辑的问题,更改以下内容:

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

到:

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

【讨论】:

  • 所以如果我有 N 个子实体:....@michael.hor257k
  • 有人可以帮我解决上述问题吗?
  • @user3560030 上面的样式表为每个&lt;Table&gt; 元素生成一个CSV“表格”——无论您可能拥有多少&lt;SubEntity&gt; 元素。如果这不能满足您的需求,您为什么不编辑您的问题并添加此类文档的示例 - 以及转换的预期结果?
猜你喜欢
  • 2016-02-13
  • 2013-06-23
  • 2022-01-09
  • 2022-01-11
  • 2011-12-31
  • 2012-02-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-22
相关资源
最近更新 更多