Solved

How can I recursively parse through XML/XSL nodes?

Posted on 2003-10-27
5
572 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
5 Comments
 
LVL 7

Expert Comment

by:_agj_
Comment Utility
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
Comment Utility
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
Comment Utility
Got it sorted out through some debugging, question solved!
0
 

Accepted Solution

by:
Inward_Spiral earned 0 total points
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now