【发布时间】: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>
</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 无效:您不能拥有像
<Table Table_A>这样的标签。 -
我刚刚分享了我的 xsl 文件