xsl-list
[Top] [All Lists]

Re: [xsl] Find the Position Index of Elements‏‏

2010-06-13 18:59:17
Hi,

  To answer the question, say that the search results returned 3
results. Then we would have

   Result 1 Something
   Result 2 Something
   Result 3 Something

  I am wondering if there is an xpath that could be created so I could
print out 1, 2, and 3 along with the other text, even though my xml
snippet:

  <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>

don't have 1 indicated here. Is this possible?
Thanks for your help.

Alice

On Sun, Jun 13, 2010 at 7:50 PM, Dimitre Novatchev 
<dnovatchev(_at_)gmail(_dot_)com> wrote:
On Sun, Jun 13, 2010 at 4:15 PM, Alice Wei 
<elite(_dot_)english(_at_)gmail(_dot_)com> wrote:
Hi, Mike:

 I want the position of the position of the searched result. For
example, when I did /music_songs/song[category='Rock'][position()<11],
I get the first 10 songs in the searched list, and I am wondering if
it is possible that I could just find out the position of the
individual xml nodes that get pulled from the search. I would like to
use that number and set it dynamically to set the pagination of
results.

Is this possible?


You still haven't defined what you understand by "position" --
relative to what and in what node list? Providing a very specific
example is really needed.


--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play





Alice

Does this make sense?

On Sun, Jun 13, 2010 at 6:58 PM, Michael Kay <mike(_at_)saxonica(_dot_)com> 
wrote:
On 13/06/2010 23:26, Alice Wei wrote:

Right, I am not intending to use this in the xsl at all, and I am
trying to use this in VB.NET in ASP.
I am trying to come up with some way so I can figure out how to
extract the position node of the elements from my xml so I can use
them to use pagination.

I tried David's solution, and somehow that didn't work, it now gives
me this error on tokenizer issues, and I am not sure if what I trying
to do is even possible at all.


It seems you want an XPath 1.0 solution and David gave you an XPath 2.0
solution.

As for me, I don't think you've explained your requirement. You talk about
wanting the "position index" or the "index id" of the elements you retrieve,
but I don't know what these terms mean. Do you want their position in the
source tree, or their position in the list of retrieved elements?

Michael Kay
Saxonica

Alice

On Sun, Jun 13, 2010 at 6:14 PM, Philip 
Fearon<pgfearo(_at_)googlemail(_dot_)com>
 wrote:


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(_dot_)english(_at_)gmail(_dot_)com>
 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.

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or 
e-mail:<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or 
e-mail:<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or 
e-mail:<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--





--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--





--
Alice Wei, MIS

Master of Information Science
Indiana University Bloomington

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--





-- 
Alice Wei, MIS

Master of Information Science
Indiana University Bloomington

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--