RE: [xsl] Making flat files strucutred hierarchically

Subject: RE: [xsl] Making flat files strucutred hierarchically
From: cknell@xxxxxxxxxx
Date: Wed, 06 Oct 2004 11:28:58 -0400
There should be plenty of examples in the archives of this list. I know that I have done several of them myself. Here is another solution to this frequently-asked question.

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output method="xml" indent="yes" encoding="UTF-8" />
  <xsl:strip-space elements="*" />

  <xsl:template match="/">
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="root">
    <xsl:apply-templates select="title" />
  </xsl:template>

  <xsl:template match="title">
    <xsl:variable name="place" select="count(preceding-sibling::title)" />
    <layer>
      <xsl:copy-of select="." />
      <xsl:apply-templates select="following-sibling::para">
        <xsl:with-param name="slot" select="$place" />
      </xsl:apply-templates>
    </layer>
  </xsl:template>

  <xsl:template match="para">
    <xsl:param name="slot" />
    <xsl:choose>
      <xsl:when test="count(preceding-sibling::title) = $slot + 1">
        <xsl:copy-of select="." />
      </xsl:when>
      <xsl:otherwise />
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
-- 
Charles Knell
cknell@xxxxxxxxxx - email



-----Original Message-----
From:     Huditsch Roman <Roman.Huditsch@xxxxxxxxxxxxx>
Sent:     Wed, 6 Oct 2004 16:36:47 +0200
To:       <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject:  [xsl] Making flat files strucutred hierarchically

Hi,

 

I am in search for a comfortable way for sructuring flat files.

Please take this source document as an example:

 

<?xml versio="1.0"?>

<root>

   <title>Titel 1</title>

  <para>Para 1</para>

  <para>Para 2</para>

  <para>Para 3</para>

  <title>Title 2</title>

  <para>Para 1</para>

  <para>Para 2</para>

  <title>Title 3</title>

  <para>Para 1</para>

  <para>Para 2</para>

</root>

 

After my XSLT 1,0 transformation the output should look like

 

<?xml versio="1.0"?>

<root>

   <layer>

     <title>Titel 1</title>

     <para>Para 1</para>

     <para>Para 2</para>

     <para>Para 3</para>

  </layer>

  <layer>

    <title>Title 2</title>

    <para>Para 1</para>

    <para>Para 2</para>

  </layer>

  <layer>  

    <title>Title 3</title>

    <para>Para 1</para>

    <para>Para 2</para>

  </layer>

</root>

 

Is there any way to do this? I don't know how to define "take all "para" nodes up to the next "title" element.....

Can you help me?

Thank you very much in advance.

 

Wbr,

Roman



**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************

Current Thread