Solved

How can I recursively parse through XML/XSL nodes?

Posted on 2003-10-27
5
580 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_
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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…

803 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