Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » XML » XSLT
Language: Relational Databases
Expertise: Beginner
Feb 2, 2000

Using XSLT to Transform Flat Data Into a Hierarchy

Question:

I have a limited understanding of XSL (Extensible Stylesheet Language) and have been trying to figure out how to write an XSL document that will transform a highly de-normalized set of data into a hierarchical XML document. It looks to me like it would be easier to transform the data with a traditional programming language. This data serves as an example of this issue:

Initial Data:
Parent 1 Key, Attribute1, Attribute2, Attribute3, Child 1 Key, Child Attribute1, Child Attribute2

Parent 1 Key, Attribute1, Attribute2, Attribute3, Child 2 Key, Child Attribute1, Child Attribute2

Parent 1 Key, Attribute1, Attribute2, Attribute3, Child 3 Key, Child Attribute1, Child Attribute2

Parent 2 Key, Attribute1, Attribute2, Attribute3, Child 1 Key, Child Attribute1, Child Attribute2

Parent 2 Key, Attribute1, Attribute2, Attribute3, Child 2 Key, Child Attribute1, Child Attribute2

Desired Transformation:
Parent 1
   Child 1
   Child 2
   Child 3
Parent 2
   Child 1
   Child 2

Answer:

If the initial data is represented in a flat structure as:









Then you actually need to do two things: attach the adult attributes uniquely to the first node, and attach the child attributes uniquely in the same fashion. Your comment about highly de-normalized data is a telling clue to solving this problem. The trick is to normalize it—create a unique array of parent elements, then iterate through each parent to retrieve the children belonging to that parent.

This is something that could be done with XSL and a lot of work, but it's probably easier to solve it using script:


Load the XML document, then pass to the function the name of the parent's primary key (here "pk") the properties that belong the parent (array("pa1","pa2","pa3") and the same for the child's primary key name and properties. You should rely on position alone, since not all parsers store attributes in the same order as they're declared.

The function returns an XML document with this format (give or take some white-space):


    
        
        
        
    
    
        
        
    

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap