Re: understanding trees

Subject: Re: understanding trees
From: Tony Graham <tgraham@xxxxxxxxxxxxxxxx>
Date: Mon, 14 Jun 1999 12:56:19 -0400 (EST)
At 14 Jun 1999 16:08 +0200, Andre Halama wrote:
 > 0. what is the difference between 'child' and 'descendant'? is it right to
 > presume that a child-node is the first node that stems from some parent
 > node while 'descendants' denote all the nodes that stem from some parent?

The child nodes are all of the immediate descendants of the context
node (i.e. one level down), and the descendants are the child nodes,
their child nodes, their child nodes' child nodes, etc., etc.

 > 1. what is the content of an element node? the spec says that 'The *value*
 > of an element node is the string that results from concatenating all
 > characters that are descendants of the element node in the order in which
 > they occur in the document.' so, if 'The children of an element node are
 > the element nodes, comment nodes, processing instruction nodes and text
 > nodes for its content.' what is meant by 'content' then? is there a
 > hierarchical or coordinating relationship between them or are they merely
 > associated nodes?

Yes, there is a hierarchical relationship.  Try the following
stylesheet on a small file.  For each node (except namespace nodes),
the output shows the type of the node and its value, then the
templates are applied to its children.  The borders around the nodes
demonstrate the containment.

------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<!-- $Id: node-type.xsl,v 1.1 1999-06-14 12:32:53-04 tkg Exp $ -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0";
                xmlns="http://www.w3.org/TR/REC-html40";
                result-ns="">

  <xsl:template match="@*">
    <div style="border: 1.5pt solid aqua; padding: 3pt">
      <p><strong>Attribute: <xsl:value-of select="local-part()"/></strong></p>
      <blockquote style="background-color: silver">
	<!-- The spans are necessary because empty attributes
	  count as attribute nodes, but Internet Explorer, at least,
	  doesn't cope very well with blockquotes containing just a
	  line break -->
	<span style="color: silver">|</span><xsl:value-of select="."/><span style="color: silver">|</span>
      </blockquote>
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template match="/">
    <div style="border: 1.5pt solid red; padding: 3pt">
      <p><strong>Root</strong></p>
      <blockquote style="background-color: silver">
	<xsl:value-of select="."/>
      </blockquote>
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template match="*">
    <div style="border: 1.5pt solid blue; padding: 3pt">
      <p><strong>Element: <xsl:value-of select="local-part()"/></strong></p>
      <blockquote style="background-color: silver">
	<!-- The spans are necessary because empty elements
	  count as element nodes, but Internet Explorer, at least,
	  doesn't cope very well with blockquotes containing nothing -->
	<span style="color: silver">|</span><xsl:value-of select="."/><span style="color: silver">|</span>
      </blockquote>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template match="text()">
    <div style="border: 1.5pt solid purple; padding: 3pt">
      <p><strong>Text</strong></p>
      <blockquote style="background-color: silver">
	<!-- The spans are necessary because line breaks between an
	  end-tag and the next start-tag count as text nodes, but
	  Internet Explorer, at least, doesn't cope very well with
	  blockquotes containing just a line break -->
	<span style="color: silver">|</span><xsl:value-of select="."/><span style="color: silver">|</span>
      </blockquote>
      <!-- This won't produce any output -->
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template match="comment()">
    <div style="border: 1.5pt solid fuchsia; padding: 3pt">
      <p><strong>Comment</strong></p>
      <blockquote style="background-color: silver">
	<xsl:value-of select="."/>
      </blockquote>
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template match="pi()">
    <div style="border: 1.5pt solid teal; padding: 3pt">
      <p><strong>Processing Instruction: <xsl:value-of select="local-part()"/></strong></p>
      <blockquote style="background-color: silver">
	<xsl:value-of select="."/>
      </blockquote>
      <xsl:apply-templates/>
    </div>
    <xsl:apply-templates/>
  </xsl:template>

</xsl:stylesheet>
------------------------------------------------------------

 > 2. am i right in assuming that 'attribute nodes' and 'namespace nodes' are
 > just associated with element nodes but don't have any *relationship* with
 > them?

Namespace nodes are a bit more fluid, but attribute nodes are each
associated with a single element node.  Isn't an association a
relationship?

Regards,


Tony Graham
======================================================================
Tony Graham                            mailto:tgraham@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9632
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


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


Current Thread