XML : XSLT 1.0 build hierarchy from parent-child relationship

I have the following flat xml structure.

  <customUI >    <elements>      <tab id="1" parent="0"/>      <tab id="-1" parent="0"/>      <group id="-2"  parent="-1"/>      <group id="2"  parent="1"/>      <group id="11"  parent="1"/>      <menu id="-27"  parent="-26"/>      <menu id="-24" parent="-4"/>      <menu id="-18" parent="-4"/>      <menu id="-11"  parent="-9"/>      <menu id="-10" parent="-9"/>      <menu id="-3"  parent="-2"/>      <menu id="3"  parent="2"/>      <menu id="10" parent="65"/>      <menu id="12"  parent="11"/>      <menu id="16"  parent="11"/>      <menu id="18" parent="11"/>      <menu id="25" parent="11" />      <menu id="29" parent="11"/>      <menu id="46" parent="11"/>      <menu id="74" parent="-3"/>      <menu id="85" parent="11"/>      <menu id="89" parent="2"/>      <menu id="95" parent="2"/>      <menu id="104"  parent="2"/>      <button id="-28"  parent="-2"/>      <button id="-25" parent="-24"/>      <button id="-12" parent="-11"/>      <button id="32" parent="29"/>      <button id="26" parent="25"/>      <button id="41" parent="18"/>      <button id="66" parent="46"/>      <button id="82"  parent="74"/>      <button id="86" parent="46"/>      <button id="87"  parent="89"/>      <button id="90"  parent="89"/>      <button id="99"  parent="89"/>      <button id="58"  parent="16"/>      <button id="42" parent="18"/>      <button id="47"  parent="46"/>      <button id="48"  parent="46"/>      <button id="33" parent="29"/>      <!-- The list continues -->    </elements>  </customUI>    

What I would like to do is to build a hierarchy using the relationship between the 'id' value and 'parent' value.

  <?xml version="1.0" encoding="utf-8"?>  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    <xsl:template match="@*|node()" name="identity">      <xsl:copy>         <xsl:apply-templates select="@*|node()"/>      </xsl:copy>  </xsl:template>    <xsl:template match="tab">      <xsl:variable name="controllerId" select="@id" as="xs:string"/>      <xsl:copy>          <xsl:apply-templates select="@*|node()"/>          <xsl:copy-of select="//*[@parent = $controllerId]"/>      </xsl:copy>  </xsl:template>      <xsl:template match="group" name="group">      <xsl:variable name="controllerId" select="@id" as="xs:string"/>      <xsl:copy>         <xsl:apply-templates select="@*|node()"/>         <xsl:copy-of select="//*[@parent = $controllerId]"/>      </xsl:copy>  </xsl:template>      <xsl:template match="menu" name="menu">      <xsl:variable name="controllerId" select="@id" as="xs:string"/>      <xsl:copy>         <xsl:apply-templates select="@*|node()"/>         <xsl:copy-of select="//*[@parent = $controllerId]"/>      </xsl:copy>  </xsl:template>    </xsl:stylesheet>    

This is what I have tried until now but it does not work.

Any help will be greatly appreciated.

No comments:

Post a Comment