Re: [xsl] How to sort this xml out

Subject: Re: [xsl] How to sort this xml out
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Wed, 9 May 2001 18:20:41 +0100
Hi Sri,

> I want to sort all the replies which previousID == a messageID and
> to group them under an tag called <content>

The first thing is that you need to be able to quickly jump from a
Message id to the Reply elements that have that Message as a parent.
So you want to index the Reply elements according to their parentID:

<xsl:key name="replies" match="Reply" use="parentID" />

Within the Main-matching template, you want to only apply templates to
the Message elements, not to the Reply elements:

<xsl:template match="Main">
   <Main>
      <xsl:apply-templates select="Message" />
   </Main>
</xsl:template>

Within the Message-matching template, you need to check whether the
Message has any replies or not, and do the appropriate thing as a
result of that check.  You can get the replies to the message using
the key that you've set up.

<xsl:template match="Message">
   <!-- retrieve the replies to the message -->
   <xsl:variable name="replies" select="key('replies', id)" />
   <xsl:choose>
      <!-- if there are any... -->
      <xsl:when test="$replies">
         <!-- ...create a Content element... -->
         <Content>
            <!-- ...copy in the content of this Message... -->
            <xsl:copy-of select="*" />
            <!-- ...and add the content of the replies... -->
            <xsl:copy-of select="$replies/*" />
         </Content>
      </xsl:when>
      <xsl:otherwise>
         <!-- otherwise, copy the whole message -->
         <xsl:copy-of select="." />
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



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


Current Thread