xsl-list
[Top] [All Lists]

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

2007-08-21 00:32:05
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.

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>