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

SV: Fwd: Flat XML File to Hierarchical ... best approa

Subject: SV: Fwd: Flat XML File to Hierarchical ... best approach?
From: "Per Osnes" <per.osnes@xxxxxx>
Date: Tue, 21 Nov 2006 14:42:33 +0100
SV:  Fwd: Flat XML File to Hierarchical ... best approa
I guess you can modify this suggestion to achieve what you want.

<xsl:template match="/Items">
  <xsl:element name="Items">
    <xsl:apply-templates select="//Item[ParentID='0000']">
      <xsl:with-param name="level" select="1"/>
    </xsl:apply-templates>
  </xsl:element>
</xsl:template>

<xsl:template match="Item">
  <xsl:param name="level"/>
  <xsl:element name="{concat(name(), $level)}">
    <xsl:copy-of select="*"/>
    <xsl:apply-templates
select="//Note[ParentID=current()/DocumentItemID]"/>
    <xsl:apply-templates select="//Item[ParentID=current()/DocumentItemID]">
      <xsl:with-param name="level" select="$level + 1"/>
    </xsl:apply-templates>
  </xsl:element>
</xsl:template>

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

As you see this solution finds an Item element and processes it's logical
Note and Item children by applying templates with a modified selection.
(The first template identifies the top node(s), you might have other search
criterias here.)

 - Per Osnes.
 

-----Opprinnelig melding-----
Fra: Mary McRae [mailto:fiberartisan@xxxxxxxxx] 
Sendt: 19. november 2006 01:56
Til: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Emne:  Fwd: Flat XML File to Hierarchical ... best approach?

First, I've been to the FAQ site and read each of the entries of Flat
File to Hierarchical. I didn't find anything that quite addressed my
problem.

My source data is flat but properly sequenced. There are two elements
whose values, if identical, imply parent/child relationship --
DocumentItemID and ParentID.

Input XML:
<Items>
<Item> (should correspond to Items/Item)
  <DocumentItemID>aaaaa</DocumentItemID>
  <ParentID>0000</ParentID>
  ...
</Item>
<Note> (should correspond to Items/Item/Note)
  <DocumentItemID>11111</DocumentItemID>
  <ParentID>aaaaa</ParentID>
  ...
</Note>
<Item> (should correspond to Items/Item/Item)
  <DocumentItemID>bbbbb</DocumentItemID>
  <ParentID>aaaaa</ParentID>
  ...
</Item>
<Item> (should correspond to Items/Item/Item/Item)
  <DocumentItemID>ccccc</DocumentItemID>
  <ParentID>bbbbb</ParentID>
  ...
</Item>

Output XML:
<Item1>
  <DocumentItemID>aaaaa</DocumentItemID>
  <ParentID>0000</ParentID>
  ...
  <Note>
    <DocumentItemID>11111</DocumentItemID>
    <ParentID>aaaaa</ParentID>
    ...
  </Note>
  <Item2>
    <DocumentItemID>bbbbb</DocumentItemID>
    <ParentID>aaaaa</ParentID>
    ...
    <Item3>
      <DocumentItemID>ccccc</DocumentItemID>
      <ParentID>bbbbb</ParentID>
      ...
    </Item3>
  </Item2>
</Item1>
</Items>

There can be as many as 8 levels of Item. There are 4 other elements
that are similar to Note and must always be a child of an Item(1-8)
element. My thought was that I could capture the Item/DocumentItemID
each time I encounter an Item element, and then each time I encounter
a Note or other child element of Item compare its child ParentID to
the Item/DocumentID and if they match output the child elements ...

I keep either suppressing output of everything but the Item elements
or repeating the Note elements in each instance of an Item element ...

Can someone point me in the right direction?

Thanks!

Judy

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.