我需要一些帮助来转换来自读取csv文件的输入数据。我想把它转换成一个更智能的结构。我在想可能有一些累积处理,但不确定如何去做。
我的输入 XML:
<lines>
<line number="1">Header, some header data 1</line>
<line number="2">Data, some data for 1</line>
<line number="3">Data, some data for 1</line>
<line number="4">Header, some header data for 2</line>
<line number="5">Data some data for 2</line>
</lines>
所需的输出是使用单词 Header 分类为块。
<?xml version="1.0" encoding="UTF-8"?>
<lines>
<Block>
<line number="1">Header, some header data 1</line>
<line number="2">Data, some data for 1</line>
<line number="3">Data, some data for 1</line>
</Block>
<Block>
<line number="4">Header, some header data for 2</line>
<line number="5">Data some data for 2</line>
</Block>
</lines>
干杯
此转换:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kFollowing" match="line[not(substring-before(.,',')='Header')]"
use="generate-id(preceding-sibling::line[substring-before(.,',')='Header'][1])"
/>
<xsl:template match="/*">
<lines>
<xsl:apply-templates select="*[substring-before(.,',')='Header']"/>
</lines>
</xsl:template>
<xsl:template match="line">
<Block>
<xsl:copy-of select=".|key('kFollowing', generate-id())"/>
</Block>
</xsl:template>
</xsl:stylesheet>
应用于提供的 XML 文档时:
<lines>
<line number="1">Header, some header data 1</line>
<line number="2">Data, some data for 1</line>
<line number="3">Data, some data for 1</line>
<line number="4">Header, some header data for 2</line>
<line number="5">Data some data for 2</line>
</lines>
产生所需的正确结果:
<lines>
<Block>
<line number="1">Header, some header data 1</line>
<line number="2">Data, some data for 1</line>
<line number="3">Data, some data for 1</line>
</Block>
<Block>
<line number="4">Header, some header data for 2</line>
<line number="5">Data some data for 2</line>
</Block>
</lines>