1

我需要一些帮助来转换来自读取 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>

干杯

4

1 回答 1

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>
于 2012-10-11T13:08:47.180 回答