Re: [xsl] Wah? Weird... Clarifying the Schema

Subject: Re: [xsl] Wah? Weird... Clarifying the Schema
From: "M. David Peterson" <m.david@xxxxxxxxxx>
Date: Thu, 28 Oct 2004 00:56:49 -0700
Hey Dr. Kay...

Thanks for the clarification on this. Looks like I need to go back and reread the spec in regards to the sort element as I obviously have developed an understanding which is incorrect.

Cheers!

<M:D/>

Michael Kay wrote:

A node exists at a particular place in a tree. Sorting changes the order in
which nodes are processed, but it doesn't modify the tree, and therefore the
axes (which navigate the tree) are unaffected by sorting.



Why is the code working?



Because XSLT sorting is defined to be stable.


You are sorting the books by category, and then outputting the first book in
each category. The rules for XSLT sorting say that duplicates retain their
original order, so the first book in a category after sorting is the same as
the first book in the category before sorting.

Actually, looking at it again, it would work even if sort order wasn't
stable, because the only property of a book that you are outputting is the
category, which of course is the same for all the books in a category.

Michael Kay
http://www.saxonica.com/



<xsl:for-each select="catalog/book">
<xsl:sort select="category" data-type="text" order="ascending" />
<xsl:if test="not(preceding-sibling::book/category = category)">
<xsl:choose>
<xsl:when test="category = $category">
<option value="{category}" selected="true"><xsl:value-of select="category" /></option>
</xsl:when>
<xsl:otherwise>
<option value="{category}"><xsl:value-of select="category" /></option>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each>

Current Thread