Re: [xsl] How to make tree menu from flat XML

Subject: Re: [xsl] How to make tree menu from flat XML
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Sun, 23 Jul 2006 22:34:36 +0530
You need to use the node-set extension function.

Something like following:

<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
     xmlns:msxsl="urn:schemas-microsoft-com:xslt">

<xsl:template match="/records">
<xsl:variable name="rtf">
<records>
  <xsl:for-each select="record[parentkeynum = 0]">
    <record>
      <xsl:variable name="key1" select="keynum" />
      <xsl:copy-of select="*" />
      <xsl:for-each select="../record[parentkeynum =
$key1]">
        <record>
          <xsl:variable name="key2" select="keynum"
/>
          <xsl:copy-of select="*" />
          <xsl:for-each
select="../record[parentkeynum = $key2]">
            <record>
              <xsl:copy-of select="*" />
            </record>
          </xsl:for-each>
        </record>
      </xsl:for-each>
    </record>
  </xsl:for-each>
</records>
</xsl:variable>

<xsl:call-template name="dve">
 <xsl:with-param name="nodeset" select="msxsl:node-set($rtf)" />
</xsl:call-template>

</xsl:template>

<xsl:template name="dve">
<xsl:param name="nodeset" />
  <xsl:for-each select="$nodeset//records/record">
    <xsl:call-template name="SubMenu">
      <xsl:with-param name="strCSS">ParentIsVisible</xsl:with-param>
    </xsl:call-template>
  </xsl:for-each>
</xsl:template>

The rest of the code would probably remain same..

Regards,
Mukul

http://gandhimukul.tripod.com

On 7/23/06, Radoslav Kolarov <roonex@xxxxxxxxx> wrote:
Thanks Mukul,
The levels are only 3 yes. But how to use heirarchy
XML made by template. This is what I trying:

<?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="/records">
 <records>
   <xsl:for-each select="record[parentkeynum = 0]">
     <record>
       <xsl:variable name="key1" select="keynum" />
       <xsl:copy-of select="*" />
       <xsl:for-each select="../record[parentkeynum =
$key1]">
         <record>
           <xsl:variable name="key2" select="keynum"
/>
           <xsl:copy-of select="*" />
           <xsl:for-each
select="../record[parentkeynum = $key2]">
             <record>
               <xsl:copy-of select="*" />
             </record>
           </xsl:for-each>
         </record>
       </xsl:for-each>
     </record>
   </xsl:for-each>
 </records>
<xsl:call-template name="dve">
</xsl:call-template>

</xsl:template>

<xsl:template name="dve" match="/">
 <xsl:for-each select="//records/record">
 <xsl:call-template name="SubMenu">
  <xsl:with-param name="strCSS">Parent
IsVisible</xsl:with-param>
 </xsl:call-template>
 </xsl:for-each>
</xsl:template>

<xsl:template name="SubMenu">
 <xsl:param name="strCSS" />



 <div class="{$strCSS}">
 <xsl:choose>
  <xsl:when test="count(record) > 0">
   <!-- Element has children, it can be expanded -->
   <input type="hidden" id="hidIsExpanded" value="0"
/>
   <label id="lblExpand" class="Expander"
onclick="ExpanderClicked()">+</label>
  </xsl:when>
  <xsl:otherwise>
   <label class="Expander"></label>
  </xsl:otherwise>
 </xsl:choose>

 <a><xsl:value-of select="keyname" /></a>
 <xsl:for-each select="record">
  <xsl:call-template name="SubMenu">
   <xsl:with-param
name="strCSS">NotVisible</xsl:with-param>
  </xsl:call-template>
 </xsl:for-each>
 </div>
</xsl:template>

</xsl:stylesheet>



and than I use this ASP

<%@ Language=VBScript %>
<% option explicit %>
<HTML>
<HEAD>
<link type="text/css" rel="stylesheet"
href="alerts.css" />
</HEAD>
<BODY>
<b>Alerts</b>
<%
       dim xmlMenu
       dim xslMenu

       'Get the source XML
       set xmlMenu = server.CreateObject("Microsoft.XMLDOM")
       xmlMenu.async = false
       xmlMenu.load server.MapPath("probe.xml")

       'Get the XSLT to transform the XML
       set xslMenu = server.CreateObject("Microsoft.XMLDOM")
       xslMenu.async = false
       xslMenu.load server.MapPath("probe.xsl")

       'Transform the source XML using XSLT
       Response.Write xmlMenu.transformNode(xslMenu)

       set xmlMenu = nothing
       set xslMenu = nothing
%>

<script language="jscript">
function ExpanderClicked()
{
       //Get the element that was clicked
       var ctlExpander = event.srcElement;
       var ctlSelectedEntry = ctlExpander.parentElement;
       //Get all the DIV elements that are direct
descendants
       var colChild = ctlSelectedEntry.children.tags("DIV");
       if(colChild.length > 0)
       {
               var strCSS;
               //Get the hidden element that indicates whether or
not entry is expanded
               var ctlHidden =
ctlSelectedEntry.all("hidIsExpanded");

               if(ctlHidden.value == "1")
               {
                       //Entry was expanded and is being contracted
                       ctlExpander.innerHTML = "+";
                       ctlHidden.value = "0";
                       strCSS = "NotVisible";
               }
               else
               {
                       //Entry is being expanded
                       ctlExpander.innerHTML = "-";
                       ctlHidden.value = "1";
                       strCSS = "IsVisible";
               }
               //Show all the DIV elements that are direct children
               for(var intCounter = 0; intCounter <
colChild.length; intCounter++)
               {
                       colChild[intCounter].className = strCSS;
               }
       }
}
</script>

</BODY>
</HTML>

But it dont working. When I trying with other XML who
is heirarchy by default it works. So my question is
how use the heirarchy XML immediantly after template
who makes it?


--- Mukul Gandhi <gandhi.mukul@xxxxxxxxx> wrote:


> If the levels would be limited to 3 only (or can
> there be any number
> of levels?), then the following stylesheet would
> work:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
>
> <xsl:output method="xml" indent="yes"/>
>
> <xsl:template match="/records">
>   <records>
>     <xsl:for-each select="record[parentkeynum = 0]">
>       <record>
>         <xsl:variable name="key1" select="keynum" />
>         <xsl:copy-of select="*" />
>         <xsl:for-each select="../record[parentkeynum
> = $key1]">
>           <record>
>             <xsl:variable name="key2"
> select="keynum" />
>             <xsl:copy-of select="*" />
>             <xsl:for-each
> select="../record[parentkeynum = $key2]">
>               <record>
>                 <xsl:copy-of select="*" />
>               </record>
>             </xsl:for-each>
>           </record>
>         </xsl:for-each>
>       </record>
>     </xsl:for-each>
>   </records>
> </xsl:template>
>
> </xsl:stylesheet>
>
> Regards,
> Mukul
>
> http://gandhimukul.tripod.com
>
> On 7/23/06, Radoslav Kolarov <roonex@xxxxxxxxx>
> wrote:
> > Mukul thanks for the answer. The problem is how to
> > turn tree structured information into dynamic tree
> > menu. I found some js scripts but I can use them
> only
> > if XML is structured heirarchy. So I have to
> transform
> > XML to this:
> >
> > <?xml version="1.0" ?>
> > - <records>
> >
> > - <record>
> >  <keynum>-100000</keynum>
> >  <keyname>FINANCIAL ALERTS (5)</keyname>
> >  <parentkeynum>0</parentkeynum>
> >  <rowcnt>5</rowcnt>
> >  <balance>0</balance>
> >  - <record>
> >    <keynum>-1</keynum>
> >    <keyname>2 CLIENTS HAVE A NEGATIVE
> > BALANCE</keyname>
> >    <parentkeynum>-100000</parentkeynum>
> >    <rowcnt>2</rowcnt>
> >    <balance>0</balance>
> >    - <record>
> >      <keynum>35</keynum>
> >      <keyname>35 MR. MICHAEL NOLAN</keyname>
> >      <parentkeynum>-1</parentkeynum>
> >      <rowcnt>1</rowcnt>
> >      <balance>-275</balance>
> >      </record>
> >    - <record>
> >      <keynum>142</keynum>
> >      <keyname>142 MR. JOHN CALINSKI</keyname>
> >      <parentkeynum>-1</parentkeynum>
> >      <rowcnt>1</rowcnt>
> >      <balance>-11</balance>
> >      </record>
> >
> >
> >    </record>
> >
> >  </record>
> >
> > </records>
> >
> > Where the elements witn parentkeynum=0 to be
> roots,
> > and elements with parentkeynum=root keynum to be
> their
> > childrens nodes, and same thing for third level.
> So
> > the question now is how to transform flat XML to
> > heirarchy XML file like this...

Current Thread