[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Need to Transform a Flat XML Struture to a Hierarchica

Subject: Need to Transform a Flat XML Struture to a Hierarchical One
From: "Kuhn, Sam (Commerce Link)" <Sam.Kuhn@xxxxxxxxx>
Date: Thu, 27 Mar 2003 05:41:17 -0500
sam kuhn
I'm using TIBCO Business Works and unsure of the XSLT processor.  I'm
attempted to create a hierarchical XML document containing nested elements
from a flat document.

Source structure:

<rows>
	<prefix>chunk of data</prefix>
	<rec_nbr>0010</rec_nbr>
	<data>another chunk of data</data>
</rows>
<rows>
	<prefix>chunk of data</prefix>
	<rec_nbr>0020</rec_nbr>
	<data>another chunk of data</data>
</rows>
<rows>
	<prefix>chunk of data</prefix>
	<rec_nbr>0030</rec_nbr>
	<data>another chunk of data</data>
</rows>
<rows>
	<prefix>chunk of data</prefix>
	<rec_nbr>0040</rec_nbr>
	<data>another chunk of data</data>
</rows>
<rows>
	<prefix>chunk of data</prefix>
	<rec_nbr>0010</rec_nbr>
	<data>another chunk of data</data>
</rows>
<rows>
	<prefix>chunk of data</prefix>
	<rec_nbr>0020</rec_nbr>
	<data>another chunk of data</data>
</rows>

...

Desired Result Structure:

<0010>
	<prefix>chunk of data</prefix>
	<rec_nbr>0010</rec_nbr>
	<data>another chunk of data</data>
</0010>
<0020>
	<prefix>chunk of data</prefix>
	<rec_nbr>0020</rec_nbr>
	<data>another chunk of data</data>
	<0030>
		<prefix>chunk of data</prefix>
		<rec_nbr>0030</rec_nbr>
		<data>another chunk of data</data>
		<0040>
			<prefix>chunk of data</prefix>
			<rec_nbr>0040</rec_nbr>
			<data>another chunk of data</data>
		</0040>
	</0030>
</0020>
<0010>
	<prefix>chunk of data</prefix>
	<rec_nbr>0010</rec_nbr>
	<data>another chunk of data</data>
</0010>
<0020>
	<prefix>chunk of data</prefix>
	<rec_nbr>0020</rec_nbr>
	<data>another chunk of data</data>
	...
</0020>

XLST So Far:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
  xmlns:pfx="http://www.tibco.com/tnt/utilitySchema"
  xmlns:pfx2="http://www.tibco.com/bw/xslt/custom-functions"
 
xmlns:pfx3="http://www.tibco.com/ns/no_namespace_schema_location/SchemaName-
2">

<xsl:variable name="data" select="/root/rows"/>

<xsl:template match="/">
   <xsl:for-each select="$data">
      <xsl:if test="rec_nbr='0010'"><xsl:call-template
name="_0010"/></xsl:if>
      <xsl:if test="rec_nbr='0020'"><xsl:call-template name="_0020">
                                    <xsl:with-param name="position"
select="position()"/>
                                    </xsl:call-template></xsl:if>
   </xsl:for-each>
</xsl:template>

<xsl:template name="_0010">
   <xsl:element name="_0010">
      <xsl:for-each select="./*">
         <xsl:copy-of select="."/>
      </xsl:for-each>
   </xsl:element>
</xsl:template>

<xsl:template name="_0020">
   <xsl:param name="position"/>

      <xsl:variable name="nextRecord" select="$data[$position+1 =
position()]"/>

      <xsl:element name="_0020">

      <xsl:copy-of select="./prefix"/>
      <xsl:copy-of select="./rec_nbr"/>
      <xsl:copy-of select="./data"/>

      <xsl:choose>
         <xsl:when test="$nextRecord/rec_nbr='0030'">
               <xsl:call-template name="_0030">
                  <xsl:with-param name="record" select="$nextRecord"/>
               </xsl:call-template>
         </xsl:when>

         <xsl:otherwise>
               <xsl:for-each select="./*">
                  <xsl:copy-of select="."/>
               </xsl:for-each>
         </xsl:otherwise>
      </xsl:choose>

      </xsl:element>

</xsl:template>


<xsl:template name="_0030">
   <xsl:param name="record"/>

   <xsl:element name="_0030">

      <xsl:copy-of select="$record/prefix"/>
      <xsl:copy-of select="$record/rec_nbr"/>
      <xsl:copy-of select="$record/data"/>

   </xsl:element>

</xsl:template>

</xsl:stylesheet>

I'm not sure how to nest a <0030> in a <0020> and a <0040> in a <0030> and
control the opening and closing of the container tags - <0020> and <0030>
being the containers for <0030> and <0040> respectively.

Any help in creating a XLST stylesheet is GREATLY appreciated.

smk


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.