[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] How to sort sibling elements based on attribute of child element


Subject: Re: [xsl] How to sort sibling elements based on attribute of child element
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 07 Jul 2009 18:30:35 -0400

Philip,

At 03:49 PM 7/7/2009, you wrote:
Thanks for the solution. The key concept that I learned was how to
select and output the preceding and following siblings of
<Annotation>.

Yes: this is a specific application of a more general principle, namely using XPath to qualify the tree traversal.


I omitted a key bit of information in my example: <Root> should have
been named <Parent>, since the parent element of Annotation is not the
document root element:

<Root>
   <Junk>junk</Junk>
   <Parent>
       <Stuff>stuff</Stuff>
       <Annotation>
           <Comment Priority="1">my first comment</Comment>
       </Annotation>
       <Annotation>
           <Comment Priority="3">my third comment</Comment>
       </Annotation>
       <Annotation>
           <Comment Priority="2">my second comment</Comment>
       </Annotation>
       <Bother>bother</Bother>
   </Parent>
</Root>

As a consequence, I didn't to use this template to produce output when
there is no Annotation:

<xsl:template match="/*[not(Annotation)]" priority="2">
 <xsl:apply-templates/>
</xsl:template>

Instead, I put a test for <Annotation> into the template - which might
not be the most elegant solution, but it works:

   <xsl:template select="Parent">
       <xsl:choose>
           <xsl:when test="Annotation">
               <xsl:apply-templates
select="Annotation[1]/preceding-sibling::*"/>
               <xsl:apply-templates select="Annotation">
                   <xsl:sort select="*/@Priority"/>
               </xsl:apply-templates>
               <xsl:apply-templates
select="Annotation[last()]/following-sibling::*"/>
           </xsl:when>
           <xsl:otherwise>
               <xsl:apply-templates select="*"/>
           </xsl:otherwise>
       </xsl:choose>
   </xsl:template>

Okay ... I had (thought I had) read that it was the document element, but that it might not be named "Root", hence the "/*" match pattern in my attempt.


In any case, you can do it either way, though mine might sometimes be microseconds faster in addition to being more elegant. :-)

Or you could match "*[Application]" instead of "Parent", to match any element that has 'Application' element children. (The complement of the principle of using XPath select expressions to qualify tree traversal is using match patterns to differentiate the handling of nodes being processed.)

Cheers,
Wendell



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


Current Thread
Keywords