[xsl] Newbie question: collapse tree

Subject: [xsl] Newbie question: collapse tree
From: "Sven Bring" <sven.bring@xxxxxxxxx>
Date: Thu, 25 Sep 2003 23:08:42 +0200 (CEST)
I have an XML file containing IE favorites and an XSL stylesheet which 
shows them in IE in a nice tree. When opening the file in IE, default 
is all folders expanded. I would like to change that, so that I get 
folders collapsed when opening the file. I've found a few threads 
about this, but cannot implement them because my stylesheet is 
slightly different (and also because I'm a complete novice in this).

Below is a portion of the XML file, just to show what kind of entries 
there are. Below that is the XSL file I want to change. I'd be very 
grateful if someone could help me with this!

I apologize for the long message,



	<parent>Bäst på nätet</parent>
	<statustime>2003-09-21, 20:31:03</statustime>
	<statustime>2003-09-21, 20:31:03</statustime>
	<statustime>2003-09-21, 20:31:03</statustime>


<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="defaultss.xsl"?>
  FavoriteSync XSL stylesheet based on
  IE5 default style sheet from Microsoft
  Author:  Johan Lundin, johan.lundin@xxxxxxxxxxx

<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl";

<xsl:template match="/">
        BODY {font:x-small 'Verdana'; margin-right:1.5em}
      <!-- container for expanding/collapsing content -->
        .c  {cursor:hand}
      <!-- button - contains +/-/nbsp -->
        .b  {color:red; font-family:'Courier New'; font-weight:bold; 
      <!-- element container -->
        .e  {margin-left:1em; text-indent:-1em; margin-right:1em}
      <!-- comment or cdata -->
        .k  {margin-left:1em; text-indent:-1em; margin-right:1em}
      <!-- tag -->
        .t  {color:#000000; font-weight:bold}
      <!-- tag in xsl namespace -->
        .xt {color:#990099}
      <!-- attribute in xml or xmlns namespace -->
        .ns {color:red}
      <!-- attribute in dt namespace -->
        .dt {color:green}
      <!-- markup characters -->
        .m  {color:blue}
      <!-- text node -->
        .tx {text-decoration: none}
      <!-- multi-line (block) cdata -->
        .db {text-indent:0px; margin-left:1em; margin-top:0px; margin-
             padding-left:.3em; border-left:1px solid #CCCCCC; 
font:small Courier}
      <!-- single-line (inline) cdata -->
        .di {font:small Courier}
      <!-- DOCTYPE declaration -->
        .d  {color:blue}
      <!-- pi -->
        .pi {color:blue}
      <!-- multi-line (block) comment -->
        .cb {text-indent:0px; margin-left:1em; margin-top:0px; margin-
             padding-left:.3em; font:small Courier; color:#888888}
      <!-- single-line (inline) comment -->
        .ci {font:small Courier; color:#888888}
        PRE {margin:0px; display:inline}

        // Detect and switch the display of CDATA and comments from an 
inline view
        //  to a block view if the comment or CDATA is multi-line.
        function f(e)
          // if this element is an inline comment, and contains more 
than a single
          //  line, turn it into a block comment.
          if (e.className == "ci") {
            if (e.children(0).innerText.indexOf("\n") > 0)
              fix(e, "cb");
          // if this element is an inline cdata, and contains more 
than a single
          //  line, turn it into a block cdata.
          if (e.className == "di") {
            if (e.children(0).innerText.indexOf("\n") > 0)
              fix(e, "db");
          // remove the id since we only used it for cleanup
          e.id = "";
        // Fix up the element as a "block" display and enable 
expand/collapse on it
        function fix(e, cl)
          // change the class name and display value
          e.className = cl;
          e.style.display = "block";
          // mark the comment or cdata display as a expandable 
          j = e.parentElement.children(0);
          j.className = "c";

          // find the +/- symbol and make it visible - the dummy link 
enables tabbing
          k = j.children(0);
          k.style.visibility = "visible";
          k.href = "#";

        // Change the +/- symbol and hide the children.  This function 
works on "element"
        //  displays
        function ch(e)
          // find the +/- symbol
          mark = e.children(0).children(0);
          // if it is already collapsed, expand it by showing the 
          if (mark.innerText == "+")
            mark.innerText = "-";
            for (var i = 1; i < e.children.length; i++)
              e.children(i).style.display = "block";
          // if it is expanded, collapse it by hiding the children
          else if (mark.innerText == "-")
            mark.innerText = "+";
            for (var i = 1; i < e.children.length; i++)
        // Change the +/- symbol and hide the children.  This function 
work on "comment"
        //  and "cdata" displays
        function ch2(e)
          // find the +/- symbol, and the "PRE" element that contains 
the content
          mark = e.children(0).children(0);
          contents = e.children(1);
          // if it is already collapsed, expand it by showing the 
          if (mark.innerText == "+")
            mark.innerText = "-";
            // restore the correct "block"/"inline" display type to 
the PRE
            if (contents.className == "db" || contents.className 
== "cb")
              contents.style.display = "block";
            else contents.style.display = "inline";
          // if it is expanded, collapse it by hiding the children
          else if (mark.innerText == "-")
            mark.innerText = "+";
            contents.style.display = "none";
        // Handle a mouse click
        function cl()
          e = window.event.srcElement;
          // make sure we are handling clicks upon expandable 
container elements
          if (e.className != "c")
            e = e.parentElement;
            if (e.className != "c")
          e = e.parentElement;
          // call the correct funtion to change the collapse/expand 
state and display
          if (e.className == "e")
          if (e.className == "k")

        // Dummy function for expand/collapse link navigation - trap 
onclick events instead
        function ex() 

        // Erase bogus link info from the status window
        function h()
          window.status=" ";

        // Set the onclick handler
        document.onclick = cl;

    <BODY class="st"><xsl:apply-templates/></BODY>


<!-- Templates for each node type follows.  The output of each 
template has a similar structure
  to enable script to walk the result tree easily for handling user 
interaction. -->
<!-- Template for the DOCTYPE declaration.  No way to get the DOCTYPE, 
so we just put in a placeholder -->
<xsl:template match="node()[nodeType()=10]">
  <DIV class="e"><SPAN>
  <SPAN class="b"><xsl:entity-ref name="nbsp"/></SPAN>
  <SPAN class="d">&lt;!DOCTYPE <xsl:node-name/><I> (View Source for 
full doctype...)</I>&gt;</SPAN>

<!-- Template for pis not handled elsewhere -->
<xsl:template match="pi()">

<!-- Template for the XML declaration.  Need a separate template 
because the pseudo-attributes
    are actually exposed as attributes instead of just element 
content, as in other pis -->
<xsl:template match="pi('xml')">

<!-- Template for attributes not handled elsewhere -->
<xsl:template match="@*" xml:space="preserve"><SPAN><xsl:attribute 
name="class"><xsl:if match="xsl:*/@*">x</xsl:if>t</xsl:attribute> 
<xsl:node-name/></SPAN><SPAN class="m">="</SPAN><B><xsl:value-
of/></B><SPAN class="m">"</SPAN></xsl:template>

<!-- Template for attributes in the xmlns or xml namespace -->
<xsl:template match="@xmlns:*|@xmlns|@xml:*"><SPAN class="ns"> 
<xsl:node-name/></SPAN><SPAN class="m">="</SPAN><B 
class="ns"><xsl:value-of/></B><SPAN class="m">"</SPAN></xsl:template>

<!-- Template for attributes in the dt namespace -->
<xsl:template match="@dt:*|@d2:*"><SPAN class="dt"> <xsl:node-
name/></SPAN><SPAN class="m">="</SPAN><B class="dt"><xsl:value-
of/></B><SPAN class="m">"</SPAN></xsl:template>

<!-- Template for text nodes -->
<xsl:template match="textNode()">
  <DIV class="e">
  <SPAN class="b"><xsl:entity-ref name="nbsp"/></SPAN>
  <SPAN class="tx"><xsl:value-of/></SPAN>

<!-- Note that in the following templates for comments and cdata, by 
default we apply a style
  appropriate for single line content (e.g. non-expandable, single 
line display).  But we also
  inject the attribute 'id="clean"' and a script call 'f(clean)'.  As 
the output is read by the
  browser, it executes the function immediately.  The function checks 
to see if the comment or
  cdata has multi-line data, in which case it changes the style to a 
expandable, multi-line
  display.  Performing this switch in the DHTML instead of from script 
in the XSL increases
  the performance of the style sheet, especially in the browser's 
asynchronous case -->
<!-- Template for comment nodes -->
<xsl:template match="comment()">
  <DIV class="k">
  <SPAN><A class="b" onclick="return false" onfocus="h()" 
STYLE="visibility:hidden">-</A> <SPAN class="m">&lt;!--</SPAN></SPAN>
  <SPAN id="clean" class="ci"><PRE><xsl:value-of/></PRE></SPAN>
  <SPAN class="b"><xsl:entity-ref name="nbsp"/></SPAN> <SPAN 

<!-- Template for cdata nodes -->
<xsl:template match="cdata()">
  <DIV class="k">
  <SPAN><A class="b" onclick="return false" onfocus="h()" 
STYLE="visibility:hidden">-</A> <SPAN class="m">&lt;![CDATA
  <SPAN id="clean" class="di"><PRE><xsl:value-of/></PRE></SPAN>
  <SPAN class="b"><xsl:entity-ref name="nbsp"/></SPAN> <SPAN 

<!-- Note the following templates for elements may examine children.  
This harms to some extent
  the ability to process a document asynchronously - we can't process 
an element until we have
  read and examined at least some of its children.  Specifically, the 
first element child must
  be read before any template can be chosen.  And any element that 
does not have element
  children must be read completely before the correct template can be 
chosen. This seems 
  an acceptable performance loss in the light of the formatting 
possibilities available 
  when examining children. -->

<!-- Template for elements not handled elsewhere (leaf nodes) -->
<xsl:template match="*">
  <DIV class="e"><DIV STYLE="margin-left:1em;text-indent:-2em">
  <SPAN class="b"><xsl:entity-ref name="nbsp"/></SPAN>
  <SPAN class="m">&lt;</SPAN><SPAN><xsl:attribute name="class"><xsl:if 
<xsl:apply-templates select="@*"/><SPAN class="m"> /&gt;</SPAN>
<!-- Template for elements with comment, pi and/or cdata children -->
<xsl:template match="*[node()]">
  <DIV class="e">
  <DIV class="c"><A href="#" onclick="return false" onfocus="h()" 
class="b">-</A> <SPAN class="m">&lt;</SPAN><SPAN><xsl:attribute 
name/></SPAN><xsl:apply-templates select="@*"/> <SPAN 
  <DIV><SPAN class="b"><xsl:entity-ref name="nbsp"/></SPAN> <SPAN 
class="m">&lt;/</SPAN><SPAN><xsl:attribute name="class"><xsl:if 

<!-- Template for elements with only text children -->
<xsl:template match="*[textNode()$and$$not$(comment()$or$pi()$or$cdata
  <DIV class="e"><DIV STYLE="margin-left:1em;text-indent:-2em">
  <SPAN class="b"> </SPAN>
    <xsl:if match="url">   
	<xsl:attribute name="href">
	<SPAN class="tx">
	<xsl:value-of select="../title"/>

<!-- Template for elements with element children -->
<xsl:template match="*[*]">
  <DIV class="e">
<xsl:if match="folder">
  <DIV class="c" STYLE="margin-left:1em;text-indent:-2em"><A href="#" 
onclick="return false" onfocus="h()" class="b">-</A> 
<SPAN><xsl:attribute name="class"><xsl:if 
select="title"/></SPAN><xsl:apply-templates select="@*"/></DIV>
  <DIV><SPAN class="b"> </SPAN></DIV>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Current Thread