Counting Nodes

Subject: Counting Nodes
From: "Maltby, David G" <david.g.maltby@xxxxxxxx>
Date: Thu, 24 Feb 2000 15:28:26 -0500
I am working on producing RTF from content marked up with DTD which I do not
control.  It contains chunks such as:

<alert>
<text type="list:num">
  <text>List Item 1</text>
  <text type="list:alpha">
    <text>List Item a</text>
    <text>List Item b</text>
  </text>
  <text>List Item 2</text>
  <text>List Item 3</text>
</alert>

And so in RTF, the expectation is that it will look something like:

  1. List Item 1
    a. List Item a
    b. List Item b
  2. List Item 2
  3. List Item 3

Problem is you can not use (child-number) for counting the siblings because
when you get to the text node "List Item 2" then (child-number) will equal
3.  So I need another way to count the text siblings.  I came up with the
following procedure:

(define (text-node-cnt #!optional (cur-text-node (current-node)))
  (let loop ( (text-siblings (siblings cur-text-node)) (cnt 0) )
    (cond
      ((node-list-empty? (my-debug text-siblings))
        (cnt))
      ((node-list=? cur-text-node (node-list-first text-siblings))
        (+ cnt 1))
      ((has-attribute? (normalize "type") (node-list-first text-siblings))
        ;don't count it
        (loop (node-list-rest text-siblings) cnt))
      (else (loop (node-list-rest text-siblings) (+ cnt 1)))
    )
  )
)

Counting-wise this works great, the problem is when looking at the text
nodes containing "List Item 1", "List Item 2", or "List Item 3",
text-siblings starts out containing 13 nodes.  The procedure exits,
respectively, on the 1st, 8th, and 12th node.  My RTF looks like:

  1. List Item 1
    a. List Item a
    b. List Item b
  8. List Item 2
  12. List Item 3

For non-built-in procedures, I have used the procedure definitions found in
the DSSSL Documentation Library.  I can not for the life of me figure out
what group of 13 nodes (siblings) is returning to me.  Any ideas on how I
might debug this?  Is there a more elegant method I have not considered?


Best Regards,
David Maltby, IETM Team, Lockheed Martin Missile and Space
"Transforming instances conforming to one DTD 
into instances conforming to another DTD" (GKH)



 DSSSList info and archive:  http://www.mulberrytech.com/dsssl/dssslist


Current Thread