RE: [xsl] Flat XML to hierarchical output ...

Subject: RE: [xsl] Flat XML to hierarchical output ...
From: christoph.naber@xxxxxxxxxxxxxxxxxxx
Date: Tue, 21 Aug 2007 09:31:28 +0200
Hello,

>  Although the XML is not hierarchical there is a hierarchy required in
> output ie
> HEADER
>  LINES
> TOTAL
>

In my current understanding of your problem I think you are trying to
aggregate the <LINE> nodes that directly follow a <HEADER> node into one
<LINES> node.
<HEADER />
<LINES>
        <LINE />
        <LINE />
</LINES>
<TOTAL />

This is possible with recursive templates.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="REPORT">
    <REPORT>
      <xsl:apply-templates select="HEADER | TOTAL" />
    </REPORT>
  </xsl:template>

  <xsl:template match="HEADER">
    <xsl:copy-of select="." />
    <LINES>
      <xsl:apply-templates select="following-sibling::*[1][self::LINE]"
mode="more2come"/>
    </LINES>
  </xsl:template>

  <xsl:template match="TOTAL" >
    <xsl:copy-of select="." />
  </xsl:template>

  <xsl:template match="LINE" mode="more2come">
    <xsl:copy-of select="." />
    <xsl:apply-templates select="following-sibling::*[1][self::LINE]"
mode="more2come" />
  </xsl:template>

</xsl:stylesheet>

OUTPUT (stylesheet applied to the given sample input)
<?xml version="1.0"?>
<REPORT>
  <HEADER/>
  <LINES>
    <LINE/>
  </LINES>
  <TOTAL/>
  <HEADER/>
  <LINES>
    <LINE/>
  </LINES>
  <TOTAL/>
  <HEADER/>
  <LINES>
    <LINE/>
    <LINE/>
    <LINE/>
  </LINES>
  <TOTAL/>
</REPORT>

Greetings Christoph


If you are not the intended addressee, please inform us immediately that you
have received this e-mail by mistake and delete it. We thank you for your
support.

Current Thread