Solved

How can I recursively parse through XML/XSL nodes?

Posted on 2003-10-27
5
592 Views
Last Modified: 2012-05-04
I've built a small VB.NET app to parse through an XSL. It works, but only to a point, because I didn't program it recursively, so it is fairly limited.

What I'd like to be able to do is to parse down to an xsl:value-of node(which I can do), and then parse recursively
back up the line until I find the comment tag that belongs to it, and return the text in the comment.

I think I'd need a function that can take in both the XML nodelist and the current node that I'm working on.

This is what I've got so far for the function, but I can't get it to work yet:

    Function getTarget(ByVal nList As XmlNodeList, ByVal node As XmlNode) As String
        Dim target As String
        Dim tmpNode As XmlNode

        For Each tmpNode In nList

            If tmpNode Is node Then

                If tmpNode.NodeType.Comment Then
                    MsgBox("Comment field!")
                    target = tmpNode.InnerText
                Else
                    MsgBox("Not a comment!")
                    tmpNode = tmpNode.ParentNode
                    target = getTarget(nList, tmpNode)
                End If

                Exit For

            End If
        Next

getTarget = target

End Function
Suggestions anyone? Thanks!
0
Comment
Question by:Inward_Spiral
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 7

Expert Comment

by:_agj_
ID: 9632277
could u make this  a lil more clear.

As in. what is this nodelist in relation to the node passed?
0
 

Author Comment

by:Inward_Spiral
ID: 9633545
Sorry, I should've been a bit clearer on the details.

Right now, upon loading the document into the VB app, I collect a nodelist of all nodes:

Dim m_xmld As XmlDocument
Dim m_nodelist XmlNodeList
m_xmld.Load(filename)
m_nodelist = m_xmld.SelectNodes("/descendant::*")

This nodelist that I'm passing through the function is the list of all the nodes that came from the file, and the node I'm passing along with it is the node that I'm currently at.

Does that clear anything up?
0
 

Author Comment

by:Inward_Spiral
ID: 9646211
Got it sorted out through some debugging, question solved!
0
 

Accepted Solution

by:
Inward_Spiral earned 0 total points
ID: 9650860
If anyone wants to try it, my function ended up like this:

    ' Maps recursively backwards to get the name of the destination node
    Function getTarget(ByVal nList As XmlNodeList, ByVal node As XmlNode) As String
        Dim target As String
        Dim tmpNode As XmlNode
        Dim i As Integer = 1

        ' Parses through the nodelist until it finds the current node
        For Each tmpNode In nList
            If tmpNode Is node Then

                ' If the node is a variable, then return the name of the variable
                If tmpNode.LocalName = "variable" Then
                    target = tmpNode.Attributes.GetNamedItem("name").Value

                    ' If the node is not a variable and begins with "xsl:",
                    ' then move up the either the previous sibling, or the parent,
                    ' whichever is available, and calls getTarget again
                ElseIf InStr(tmpNode.Name, "xsl:") Then

                    If tmpNode.PreviousSibling Is Nothing Then
                        tmpNode = tmpNode.ParentNode
                        target = getTarget(nList, tmpNode)

                    Else
                        tmpNode = tmpNode.PreviousSibling
                        target = getTarget(nList, tmpNode)

                    End If

                    ' If it is neither a variable or begins with "xsl:", it must be
                    ' what we're looking for. Return the node as the target name.
                Else
                    target = tmpNode.Name
                    'MsgBox("Target is found! It is: " + target)
                End If

                Exit For
            End If
        Next

        getTarget = target
    End Function
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question