I'm new to the forum and to xslt.

My problem is that I have to transform a cloud-like data hierarchy xml into a tree-like data hierarchy xml. Let me give you some example:

input xml:


    <OBJECT id="1" class="CAR">
      <ATTRIBUTE name="name" value="CITROEN CX" /> 
      <ATTRIBUTE name="content" value="photo_link" /> 
    <OBJECT id="2" class="SPARE_PARTS">
      <ATTRIBUTE name="name" value="REAR_LEFT_WING" /> 
      <ATTRIBUTE name="content" value="photo_link" /> 
    <OBJECT id="3" class="CAR">
      <ATTRIBUTE name="name" value="Peugeot 206" /> 
      <ATTRIBUTE name="content" value="photo_link" /> 
    <OBJECT id="4" class="SPARE_PARTS">
      <ATTRIBUTE name="name" value="SPARK_PLUG" /> 
      <ATTRIBUTE name="content" value="photo_link" /> 
    <LINK from="1" to="2" /> 
    <LINK from="3" to="4" /> 

the example output is:

<TreeNode id="1" class="CAR" name="Citroen CX">
  <TreeNode id="2" class="SPARE_PARTS" name="REAR_LEFT_WING"/>
<TreeNode id="3" class="CAR" name="Peugeot 206">
  <TreeNode id="4" class="SPARE_PARTS" name="SPARK_PLUG"/>

the question is:
is it realizable by means of XSLT?
If so, is it worth doing this way?
And finally, any clue where to start from ? :)

Thanks for your help in advance!

controled by links
and rekursiv

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" indent="yes"/>
	<xsl:template match="/">
		<xsl:apply-templates select="root"/>
	<xsl:template match="root">
		<xsl:apply-templates select="LINKS"/>
	<xsl:template match="LINKS">
		<xsl:apply-templates select="LINK"/>
	<xsl:template match="LINK" name="links">
		<xsl:param name="from" select="@from"/>
		<xsl:param name="to" select="@to"/>
		<xsl:if test="$from &lt;= $to">
				<xsl:attribute name="id">
					<xsl:value-of select="$from"/>
				<xsl:attribute name="class">
					<xsl:value-of select="//OBJECT[@id=$from]/@class"/>
				<xsl:attribute name="name">
					<xsl:value-of select="//OBJECT[@id=$from]/ATTRIBUTE/@value"/>
				<xsl:call-template name="links">
					<xsl:with-param name="from" select="$from+1"/>
					<xsl:with-param name="to" select="$to"/>

Helmut Hagemann

This article has been dead for over six months. Start a new discussion instead.