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>

Thiis is one of the songs out of 5 categories I have, 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, so I can use that number to determine a pagination control I am creating. How can I do that?

Thanks for your help.

Recommended Answers

All 5 Replies

Your question is not very clear to me. An example of what you want would help.

Sorry about that, let me try here again.

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 position of the search result from the xml. 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 find out the position of the
individual xml nodes that get pulled from the search.

Is there a particular expression I need to use here?
Thanks for your help.

http://stackoverflow.com/questions/226405/find-position-of-a-node-using-xpath

Thanks for this, but it looks like I have done something seriously wrong here that may need some of the members on this forum's help.

Here is my updated code:

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.XML" %>

<script runat="server">   
   
    Sub submit(ByVal sender As Object, ByVal e As EventArgs)
        songNodesOut.Text = String.Empty

        Dim file As String = Context.Server.MapPath("alice_music_list2.xml")
        Dim document As XmlDocument = New XmlDocument()
        document.Load(file)
        
        Dim songList As XmlNodeList
        songList = document.SelectNodes("/music_songs/song[category='" & drop1.SelectedItem.Text & "']")
     
        Dim songDisplay As XmlNodeList       
        songDisplay = document.SelectNodes("/music_songs/song[category='" & drop1.SelectedItem.Text & "'][position()<11]")
        
        Dim songDisplay2 As XmlNodeList        
        songDisplay2 = document.SelectNodes("count(/music_songs/song[category='" & drop1.SelectedItem.Text & "']/precedng-

sibling::*)+1")
       
 Dim count As Integer = songList.Count
        Dim page AS Decimal = songList.Count/10     

        Dim IntegerPart As Integer= Int(page) 
        Dim DecimalPart As Decimal = page - IntegerPart 
        if(DecimalPart >0) Then
           IntegerPart+=1
        End If


        mess.Text = "<p>You selected <b>" & drop1.SelectedItem.Text & " " & count & " items</b>. That is " & IntegerPart &  

" pages</p>"
 
        Dim song As XmlNode   
        Dim song_num As XmlNode
 
        For Each song In songDisplay
        For Each song_num In songDisplay2
           
            Dim number As XmlNode = song_num.FirstChild   
            songNodesOut.Text &= "<br /><b>ID:</b> " & number.InnerText & "<br />"          

            Dim title As XmlNode = song.FirstChild
            songNodesOut.Text &= "<br /><b>Title:</b> " & title.InnerText & "<br/>"

            Dim category As XmlNode = title.NextSibling
            songNodesOut.Text &= "<b>Category:</b> " & category.InnerText & "<br />"
    
            Dim album As XmlNode = category.NextSibling
            Dim artist As XmlNode = album.NextSibling
            songNodesOut.Text &= "<b>Artist:</b> " & artist.InnerText & "<br/>"
            songNodesOut.Text &= "<b>Album:</b> " & album.InnerText & "<br />"
   
            Dim date1 As XmlNode = artist.NextSibling
            Dim dateAttribute As XmlAttribute = date1.Attributes("added")
            songNodesOut.Text &= "<b>Date Added to Collection:</b> " & dateAttribute.Value & "<br/><hr/>"
         
           Next
     Next	
	
    End Sub
</script>
<html>
<head/>
            <body>            
             <div id="main">
	    <form id="Form1" runat="server">
            <asp:DropDownList id="drop1" runat="server">
            <asp:ListItem>Light Rock</asp:ListItem>
            <asp:ListItem>Rock</asp:ListItem>
            </asp:DropDownList>
           <asp:Button ID="Button1" Text="Submit" OnClick="submit" runat="server"/>
          <asp:Label id="songNodesOut" runat="server" />

</form>
</body>
</html>

When I run the code, I can see the drop down menus alright, but once I make one of the selections, like Rock, even though from my above example that such song exists, and yet I get this error:

Server Error in '/' Application.
'count(/music_songs/song[category='Rock']/precedng-sibling::*)+1' has an invalid token.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Xml.XPath.XPathException: 'count(/music_songs/song[category='Rock']/precedng-sibling::*)+1' has an invalid token.

I am sure that I have done something wrong here, but what could it be?
Thanks for your help.

This is what I come up with, does anyone think this is applicable if I use this for pagination purposes?

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.XML" %>

<script runat="server">   
   
    Sub submit(ByVal sender As Object, ByVal e As EventArgs)
        songNodesOut.Text = "" 

        Dim file As String = Context.Server.MapPath("music.xml")
        Dim document As XmlDocument = New XmlDocument()
        document.Load(file)
        
        Dim songList As XmlNodeList
        songList = document.SelectNodes("/music_songs/song[category='" & drop1.SelectedItem.Text & "']")
     
        Dim songDisplay As XmlNodeList       
        songDisplay = document.SelectNodes("/music_songs/song[category='" & drop1.SelectedItem.Text & "'][position()<11]")
        
        Dim count As Integer = songList.Count
        Dim page AS Decimal = songList.Count/10     

        Dim IntegerPart As Integer= Int(page) 
        Dim DecimalPart As Decimal = page - IntegerPart 
        if(DecimalPart >0) Then
           IntegerPart+=1
        End If


        mess.Text = "<p>You selected <b>" & drop1.SelectedItem.Text & " " & count & " items</b>. That is " & IntegerPart &  " pages</p>"
 
        Dim song As XmlNode   
        Dim d As Integer
  
            For Each song In songList     
            For d=1 to count
         
            songNodesOut.Text &= "<br /><b>ID:</b> " & d & "<br />"    
            Dim title As XmlNode = song.FirstChild
            songNodesOut.Text &= "<b>Title:</b> " & title.InnerText & "<br/>"

            Dim category As XmlNode = title.NextSibling
            songNodesOut.Text &= "<b>Category:</b> " & category.InnerText & "<br />"
    
            Dim album As XmlNode = category.NextSibling
            Dim artist As XmlNode = album.NextSibling
            songNodesOut.Text &= "<b>Artist:</b> " & artist.InnerText & "<br/>"
            songNodesOut.Text &= "<b>Album:</b> " & album.InnerText & "<br />"
   
            Dim date1 As XmlNode = artist.NextSibling
            Dim dateAttribute As XmlAttribute = date1.Attributes("added")
            songNodesOut.Text &= "<b>Date Added to Collection:</b> " & dateAttribute.Value & "<br/><hr/>" 
     Next d         
     Next 
	
    End Sub
</script>
<html>
<head/>
            <body>
             <div id="main">
	    <form id="Form1" runat="server">
            <asp:DropDownList id="drop1" runat="server">
            <asp:ListItem>Light Rock</asp:ListItem>
            <asp:ListItem>Rock</asp:ListItem>
            </asp:DropDownList>
           <asp:Button ID="Button1" Text="Submit" OnClick="submit" runat="server"/>
           <asp:Label id="songNodesOut" runat="server" />

</form>
</body>
</html>

Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.