?
Solved

How can I recursively parse through XML/XSL nodes?

Posted on 2003-10-27
5
Medium Priority
?
612 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
  • 3
4 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

807 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