[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How can I recursively parse through XML/XSL nodes?

Posted on 2003-10-27
5
Medium Priority
?
608 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

656 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