我需要一些帮助来转换来自读取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>
欢呼声
答案 0 :(得分:0)
此转化:
<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>