Hi!

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:

<DATA>

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

the example output is:

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

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>
	<xsl:template match="root">
		<xsl:apply-templates select="LINKS"/>
	</xsl:template>
	<xsl:template match="LINKS">
		<xsl:apply-templates select="LINK"/>
	</xsl:template>
	<xsl:template match="LINK" name="links">
		<xsl:param name="from" select="@from"/>
		<xsl:param name="to" select="@to"/>
		<xsl:if test="$from &lt;= $to">
			<treenode>
				<xsl:attribute name="id">
					<xsl:value-of select="$from"/>
				</xsl:attribute>
				<xsl:attribute name="class">
					<xsl:value-of select="//OBJECT[@id=$from]/@class"/>
				</xsl:attribute>
				<xsl:attribute name="name">
					<xsl:value-of select="//OBJECT[@id=$from]/ATTRIBUTE/@value"/>
				</xsl:attribute>				
				<xsl:call-template name="links">
					<xsl:with-param name="from" select="$from+1"/>
					<xsl:with-param name="to" select="$to"/>
				</xsl:call-template>
			</treenode>
		</xsl:if>
	</xsl:template>
</xsl:stylesheet>

Helmut Hagemann

Thank you so much, you are a life saver :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.