Subject: Re: [xsl] Find the Position Index of Elements From: Philip Fearon <pgfearo@xxxxxxxxxxxxxx> Date: Sun, 13 Jun 2010 23:14:47 +0100 |
Though this is the xsl-list, you haven't mentioned XSLT, so this answer is just in case you wanted an XPath / .NET solution: XPath 1.0 as available natively in VB.NET can't return the sequence of atomic numbers you describe, it can only return a single value or a nodeset. If, however, you were using VB.NET with an XPath 2.0 processor (such as Saxon.NET) you could simply use: for $rocksong in /music_songs/song[category = 'Rock'] return $rocksong/count(preceding-sibling::song) + 1 Going back to XPath 1.0: you would first need to iterate through all the returned song elements returned by your expression and then, using another expression, evaluate the current song node position relative to previous song nodes: count(./preceding-sibling::song) I've shown below sample code of how you would use the .NET XPathNavigator to work with the context node in this case. This sample is in C# but should be easy enough to convert to VB.Net. XPathDocument xdoc = new XPathDocument("c:\\test\\songs.xml"); XPathNavigator xnav = xdoc.CreateNavigator(); XPathExpression songsExpr = xnav.Compile("/music_songs/song[category='Rock']"); XPathExpression countSongsExpr = xnav.Compile("count(./preceding-sibling::song)"); XPathNodeIterator iterator = (XPathNodeIterator)xnav.Evaluate(songsExpr); while(iterator.MoveNext()) { XPathNavigator songNav = (XPathNavigator)iterator.Current.Clone(); double songPosition = (double)songNav.Evaluate(countSongsExpr) + 1; } The above XPath 1.0 sample works, but hopefully this also shows how much simpler (and more readable) things would be with XPath 2.0 Regards Phil Fearon http://qutoric.com/ On Sun, Jun 13, 2010 at 8:43 PM, Alice Wei <elite.english@xxxxxxxxx> wrote: > Hi, > > I have an XML snippet as in the following: > > <music_songs> > <song> > <title>(I Just) Died In Your Arms</title> > <category>Rock</category> > <album>80 Popular Hits</album> > <artist>Cutting Crew</artist> > <date added="03-24-2009"/> > </song> > </music_songs> > > This is one of the songs out of categories I have in my xml file, and > currently I use XPath expression as in the following: > /music_songs/song[category='Rock' as an example to find all the songs > in the Rock category. > > I need to also be able to pull the position index of the song elements > that I pull from the above expression, and use that in VB.NET for > process. I tried using count(), position(), but they seem to be able > to detect, but they cannot give me a list like > > 1 > 2 > 3 > > for the index id of the search list. Is there a particular expression > I need to use here? > > Thanks for your help.
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Find the Position Index o, David Carlisle | Thread | Re: [xsl] Find the Position Index o, Alice Wei |
Re: [xsl] distinguish whether varia, TW | Date | Re: [xsl] Find the Position Index o, Alice Wei |
Month |