Selecting Unique Elements Options
Sunday Ironfoot
Posted: Friday, November 21, 2008 1:07:44 PM
Rank: Newbie

Joined: 10/8/2007
Posts: 21
I'm trying to select unique elements using XSLT and XPath (remove duplicates). If I have...

Code:
<keywords>
     <keyword>Apple</keyword>
     <keyword>Apple</keyword>
     <keyword>Orange</keyword>
     <keyword>Pear</keyword>
     <keyword>Banana</keyword>
     <keyword>Banana</keyword>
     <keyword>Grapes</keyword>
     <keyword>Grapes</keyword>
     <keyword>Apple</keyword>
     <keyword>Banana</keyword>
</keywords>

I want to return...

Code:
<keywords>
     <keyword>Apple</keyword>
     <keyword>Orange</keyword>
     <keyword>Pear</keyword>
     <keyword>Banana</keyword>
     <keyword>Grapes</keyword>
</keywords>


Any ideas? I've tried a few examples on the web that don't seem to work. (eg.
Code:
//keywords/keyword[not(text()=preceding::text())]
)

Dominic Pettifer
drobar
Posted: Friday, November 21, 2008 7:01:17 PM

Rank: Umbracoholic

Joined: 9/8/2006
Posts: 1,831
Location: MA, USA
leekelleher
Posted: Sunday, November 23, 2008 6:37:11 PM

Rank: Enthusiast

Joined: 5/28/2008
Posts: 45
Location: Bristol, UK
Hi Dominic,

Try this:

Code:
/keywords/keyword[not(. = preceding::keyword)]


If you want to know why your XPath wasn't working previous, it was due to the context of the "preceding::text()"; it was testing against itself, rather than the other "keyword" values.

Hope that helps.

Cheers,
- Lee


http://leekelleher.com/
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.