我需要创建一个XSLT来将CSV(逗号分隔的文件)转换为分层XML。
这是输入文件:
<root>
L11,L12,L21,L22,L31,L32
1,A,1,C,1,G
1,A,1,C,2,H
1,A,2,D,1,I
1,A,2,D,2,J
2,B,1,E,1,K
2,B,1,E,2,L
2,B,2,F,1,M
2,B,2,F,2,N
</root>
这是所需的输出XML:
<?xml version="1.0" encoding="utf-8"?>
<Document>
<Level1>
<L11>1</L11>
<L12>A</L12>
<Level2>
<L21>1</L11>
<L22>C</L12>
<Level3>
<L31>1</L31>
<L32>G</L32>
</Level3>
<Level3>
<L31>2</L31>
<L32>H</L32>
</Level3>
</Level2>
<Level2>
<L21>2</L11>
<L22>D</L12>
<Level3>
<L31>1</L31>
<L32>I</L32>
</Level3>
<Level3>
<L31>2</L31>
<L32>J</L32>
</Level3>
</Level2>
</Level1>
<Level1>
<L11>2</L11>
<L12>B</L12>
<Level2>
<L21>1</L11>
<L22>E</L12>
<Level3>
<L31>1</L31>
<L32>K</L32>
</Level3>
<Level3>
<L31>2</L31>
<L32>L</L32>
</Level3>
</Level2>
<Level2>
<L21>2</L11>
<L22>F</L12>
<Level3>
<L31>1</L31>
<L32>M</L32>
</Level3>
<Level3>
<L31>2</L31>
<L32>N</L32>
</Level3>
</Level2>
</Level1>
</Document>
我一直试图在网上找到一些例子,但找不到类似的例子。我以前从未做过XSLT转换,所以如果您能为我指明正确的方向,我将不胜感激。
更新1:我正在考虑两步转换。例如,第一步是将CSV转换为XML:
<?xml version="1.0" encoding="utf-8"?>
<Document>
<row><L11>1</L11><L12>A</L12><L21>1</L12><L31>C</L31><L32>1</L31><L32>G</L32></row>
<row><L11>1</L11><L12>A</L12><L21>1</L12><L31>C</L31><L32>2</L31><L32>H</L32></row>
<row><L11>1</L11><L12>A</L12><L21>2</L12><L31>D</L31><L32>1</L31><L32>I</L32></row>
<row><L11>1</L11><L12>A</L12><L21>2</L12><L31>D</L31><L32>2</L31><L32>J</L32></row>
<row><L11>2</L11><L12>B</L12><L21>1</L12><L31>E</L31><L32>1</L31><L32>K</L32></row>
<row><L11>2</L11><L12>B</L12><L21>1</L12><L31>E</L31><L32>2</L31><L32>L</L32></row>
<row><L11>2</L11><L12>B</L12><L21>2</L12><L31>F</L31><L32>1</L31><L32>M</L32></row>
<row><L11>2</L11><L12>B</L12><L21>2</L12><L31>F</L31><L32>2</L31><L32>N</L32></row>
</Document>
第二步是使用某种分组将XML转换为所需的格式。如果没有其他方法可以实现的话,我不介意进行两次转换。
有什么建议吗?
更新2:将使用Microsoft.NET Framework XSLT处理器。
如果抽象的例子很难阅读,你可以在这里看到所需转换的真实例子:http://servingxml.sourceforge.net/examples/#timesheets-例如
据我所知,使用单一转换是不可能的,所以如果有人能向我展示如何将XML从Update1格式转换为所需的XML格式,那么一半的工作就完成了,我会接受这个答案。
为了清晰起见,我稍微修改了您的输入,这样标签就有意义了:
XML
<root>
GroupName,GroupValue,SubGroupName,SubGroupValue,ItemName,ItemValue
1,A,1,C,1,G
1,A,1,C,2,H
1,A,2,D,1,I
1,A,2,D,2,J
2,B,1,E,1,K
2,B,1,E,2,L
2,B,2,F,1,M
2,B,2,F,2,N
</root>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="k1" match="row" use="cell[1]"/>
<xsl:key name="k2" match="row" use="concat(cell[1], '|', cell[3])"/>
<xsl:template match="/">
<!-- tokenize csv -->
<xsl:variable name="rows">
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="root"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="data">
<xsl:for-each select="exsl:node-set($rows)/row[position() > 1]">
<row>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="."/>
<xsl:with-param name="delimiter" select="','"/>
<xsl:with-param name="name" select="'cell'"/>
</xsl:call-template>
</row>
</xsl:for-each>
</xsl:variable>
<!-- output -->
<document>
<xsl:for-each select="exsl:node-set($data)/row[count(. | key('k1', cell[1])[1]) = 1]">
<group>
<name>
<xsl:value-of select="cell[1]"/>
</name>
<value>
<xsl:value-of select="cell[2]"/>
</value>
<xsl:for-each select="key('k1', cell[1])[count(. | key('k2', concat(cell[1], '|', cell[3]))[1]) = 1]">
<subgroup>
<name>
<xsl:value-of select="cell[3]"/>
</name>
<value>
<xsl:value-of select="cell[4]"/>
</value>
<items>
<xsl:for-each select="key('k2', concat(cell[1], '|', cell[3]))">
<item>
<name>
<xsl:value-of select="cell[5]"/>
</name>
<value>
<xsl:value-of select="cell[6]"/>
</value>
</item>
</xsl:for-each>
</items>
</subgroup>
</xsl:for-each>
</group>
</xsl:for-each>
</document>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="' '"/>
<xsl:param name="name" select="'row'"/>
<xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
<xsl:if test="$token">
<xsl:element name="{$name}">
<xsl:value-of select="$token"/>
</xsl:element>
</xsl:if>
<xsl:if test="contains($text, $delimiter)">
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
<xsl:with-param name="name" select="$name"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="UTF-8"?>
<document>
<group>
<name>1</name>
<value>A</value>
<subgroup>
<name>1</name>
<value>C</value>
<items>
<item>
<name>1</name>
<value>G</value>
</item>
<item>
<name>2</name>
<value>H</value>
</item>
</items>
</subgroup>
<subgroup>
<name>2</name>
<value>D</value>
<items>
<item>
<name>1</name>
<value>I</value>
</item>
<item>
<name>2</name>
<value>J</value>
</item>
</items>
</subgroup>
</group>
<group>
<name>2</name>
<value>B</value>
<subgroup>
<name>1</name>
<value>E</value>
<items>
<item>
<name>1</name>
<value>K</value>
</item>
<item>
<name>2</name>
<value>L</value>
</item>
</items>
</subgroup>
<subgroup>
<name>2</name>
<value>F</value>
<items>
<item>
<name>1</name>
<value>M</value>
</item>
<item>
<name>2</name>
<value>N</value>
</item>
</items>
</subgroup>
</group>
</document>
注意:
元素名称被硬编码到样式表中,而不是从输入中提取(尽管这也可能需要更多的努力);
您可能需要使用
msxsl:node-set()
函数而不是EXSLT函数。
好吧,您提供的数据不是XML,所以要解决这个问题,您至少需要像Saxon 9、XmlPrime、Exselt或Altova那样实现XSLT2.0。我认为在第一步中可以将逗号分隔行中的数据转换为XML元素,然后在第二步中可以使用分组来转换输入。在我看来,XSLT3.0中支持的复合分组密钥使用可能会有所帮助,因此以下是XSLT3.0:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
xmlns:array="http://www.w3.org/2005/xpath-functions/array"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="xs math array mf" version="3.0">
<xsl:param name="sep" as="xs:string" select="','"/>
<xsl:output indent="yes"/>
<xsl:function name="mf:nest" as="element(Level)*">
<xsl:param name="levels" as="xs:string*"/>
<xsl:for-each-group select="$levels" group-by="substring(., 1, 1)">
<xsl:if test="current-grouping-key() != ''">
<Level index="{current-grouping-key()}">
<xsl:sequence select="mf:nest(current-group() ! substring(., 2))"/>
</Level>
</xsl:if>
</xsl:for-each-group>
</xsl:function>
<xsl:function name="mf:group" as="element()*">
<xsl:param name="rows" as="element(row)*"/>
<xsl:param name="levels" as="element(Level)*"/>
<xsl:param name="index" as="xs:integer"/>
<xsl:variable name="current-level" as="element(Level)?" select="$levels[1]"/>
<xsl:if test="$current-level">
<xsl:variable name="indices-of-current-level" select="$current-level/Level/@index!(. + $index)"/>
<xsl:for-each-group select="$rows" group-by="cell[position() = $indices-of-current-level]" composite="yes">
<Level index="{$current-level/@index}">
<xsl:for-each select="current-grouping-key()">
<Data index="L{$current-level/@index}{position()}">
<xsl:value-of select="."/>
</Data>
</xsl:for-each>
<xsl:sequence select="mf:group(current-group(), $levels[position() gt 1], $index + count(current-grouping-key()))"/>
</Level>
</xsl:for-each-group>
</xsl:if>
</xsl:function>
<xsl:template match="root">
<xsl:variable name="lines" select="tokenize(., '(r?n)+')[normalize-space()]"/>
<xsl:variable name="levels" select="tokenize(normalize-space($lines[1]), $sep)"/>
<xsl:variable name="nesting" select="mf:nest($levels ! substring(., 2))"/>
<xsl:variable name="data" select="$lines[position() gt 1]"/>
<xsl:variable name="rows" as="element(row)*">
<xsl:for-each select="$data">
<row>
<xsl:for-each select="tokenize(normalize-space(), $sep)">
<cell>
<xsl:value-of select="."/>
</cell>
</xsl:for-each>
</row>
</xsl:for-each>
</xsl:variable>
<document>
<!-- only for debugging respectively to show the intermediate XML data structure used for further processing -->
<xsl:copy-of select="$nesting"/>
<xsl:copy-of select="$rows"/>
<xsl:sequence select="mf:group($rows, $nesting, 0)"/>
</document>
</xsl:template>
</xsl:stylesheet>
这给出了的结果
<?xml version="1.0" encoding="UTF-8"?>
<document>
<Level index="1">
<Level index="1"/>
<Level index="2"/>
</Level>
<Level index="2">
<Level index="1"/>
<Level index="2"/>
</Level>
<Level index="3">
<Level index="1"/>
<Level index="2"/>
</Level>
<row>
<cell>1</cell>
<cell>A</cell>
<cell>1</cell>
<cell>C</cell>
<cell>1</cell>
<cell>G</cell>
</row>
<row>
<cell>1</cell>
<cell>A</cell>
<cell>1</cell>
<cell>C</cell>
<cell>2</cell>
<cell>H</cell>
</row>
<row>
<cell>1</cell>
<cell>A</cell>
<cell>2</cell>
<cell>D</cell>
<cell>1</cell>
<cell>I</cell>
</row>
<row>
<cell>1</cell>
<cell>A</cell>
<cell>2</cell>
<cell>D</cell>
<cell>2</cell>
<cell>J</cell>
</row>
<row>
<cell>2</cell>
<cell>B</cell>
<cell>1</cell>
<cell>E</cell>
<cell>1</cell>
<cell>K</cell>
</row>
<row>
<cell>2</cell>
<cell>B</cell>
<cell>1</cell>
<cell>E</cell>
<cell>2</cell>
<cell>L</cell>
</row>
<row>
<cell>2</cell>
<cell>B</cell>
<cell>2</cell>
<cell>F</cell>
<cell>1</cell>
<cell>M</cell>
</row>
<row>
<cell>2</cell>
<cell>B</cell>
<cell>2</cell>
<cell>F</cell>
<cell>2</cell>
<cell>N</cell>
</row>
<Level index="1">
<Data index="L11">1</Data>
<Data index="L12">A</Data>
<Level index="2">
<Data index="L21">1</Data>
<Data index="L22">C</Data>
<Level index="3">
<Data index="L31">1</Data>
<Data index="L32">G</Data>
</Level>
<Level index="3">
<Data index="L31">2</Data>
<Data index="L32">H</Data>
</Level>
</Level>
<Level index="2">
<Data index="L21">2</Data>
<Data index="L22">D</Data>
<Level index="3">
<Data index="L31">1</Data>
<Data index="L32">I</Data>
</Level>
<Level index="3">
<Data index="L31">2</Data>
<Data index="L32">J</Data>
</Level>
</Level>
</Level>
<Level index="1">
<Data index="L11">2</Data>
<Data index="L12">B</Data>
<Level index="2">
<Data index="L21">1</Data>
<Data index="L22">E</Data>
<Level index="3">
<Data index="L31">1</Data>
<Data index="L32">K</Data>
</Level>
<Level index="3">
<Data index="L31">2</Data>
<Data index="L32">L</Data>
</Level>
</Level>
<Level index="2">
<Data index="L21">2</Data>
<Data index="L22">F</Data>
<Level index="3">
<Data index="L31">1</Data>
<Data index="L32">M</Data>
</Level>
<Level index="3">
<Data index="L31">2</Data>
<Data index="L32">N</Data>
</Level>
</Level>
</Level>
</document>
使用Oxygen 18中的Saxon PE 9.6或Altova XMLSpy 2017中的XSLT 3.0处理器。当然,您可以删除输出中间数据结构的行,也可以更改最终XML的创建,以输出名称中包含级别的元素名称,但我更喜欢使用可以由模式表示的名称,并将任何计数器或级别索引放入属性中。