Subject: Re: [xsl] Finding Unique Nodes From: Peter Davis <pdavis152@xxxxxxxxx> Date: Mon, 1 Apr 2002 22:24:41 -0800 |
On Monday 01 April 2002 21:04, Ivan Pedruzzi wrote: > <?xml version="1.0" encoding="UTF-8"?> > <xsl:stylesheet version="1.0" > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> > <xsl:output method="xml"/> > <xsl:template match="/"> > <list> > <xsl:for-each select="list/item"> > <xsl:sort select="list/item"/> This sort requires that "/list/item/list/item" exists, "list/item" is relative to the "list/item" that was selected in the for-each. To do what I think you are trying to do, you want: <xsl:sort select="."/> > <xsl:if test="not(following-sibling::item = .)"> The test for following-sibling::item is applied on the source document, *not* the document after it has been sorted. So the sorting will not affect anything. Also, this is the same as: not(./following-sibling::item = .) which is always true, because a node is never a following-sibling of itself. Therefore, the next line (<item><xsl:value-of select="."/></item>) will always be executed. > <item><xsl:value-of select="."/></item> > </xsl:if> > </xsl:for-each> > </list> > </xsl:template> > </xsl:stylesheet> There is a way to select unique nodes with following-sibling, but I prefer the key()-based solution myself. <xsl:template match="item"> <xsl:if test="not(following-sibling::item[string(.) = string(current())])"> <xsl:copy-of select="."/> </xsl:if> </xsl:template> This doesn't easily allow for counting the number of unique nodes (it only lets you output their values). Not to mention that it doesn't scale with large documents, which is why you should do something like this: <xsl:key name="item" match="item" use="string(.)"/> <xsl:template match="/"> <xsl:text>Unique items: </xsl:text> <xsl:value-of select="count(list/item[count(key('item', string(.)) | .) = 1])"/> </xsl:template> Look up "Muenchian method" for info on why this works. -- Peter Davis The Angels want to wear my red shoes. -- E. Costello XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] Finding Unique Nodes, Ivan Pedruzzi | Thread | RE: [xsl] Finding Unique Nodes, Ivan Pedruzzi |
[xsl] removing duplicates in loops, Yi Qin | Date | [xsl] Writing a stylesheet to creat, Kunal H. Parikh |
Month |