XPath with recursive definitions

Viewed 335

I have a DTD like this :

     <!ELEMENT Root (Thread*) >
     <!ELEMENT Thread(ThreadId, Message) >
    <!ELEMENT Replies(message+) >
     <!ELEMENT message(timestamp, sender, recipient, subject, text, Replies?)>

So a thread will have a message and this message can have a node 'replies', then this node can contain messages and so on until the bottom of the structure.

Now what I want to do is to first retrieve the ID of the thread with the most messages and then retrieve the ID of the thread with the longest chain of nested replies.

It feels like a recursive problem but I'm not able to approach it in XPath. So far I tried something like this :

      For $thread in //thread
      Count(descendant-or-self::$thread/message) 

For each thread I Try to count the number of children messages nodes, but this solution counts the number of All the children nodes of the thread, therefore including Replies nodes.

I'm feeling lost with this kind of problems as I cannot figure out what to do in these 'recursive situations'.

1 Answers
Related