Announcement

Collapse
No announcement yet.

Any XPath gurus here?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Any XPath gurus here?

    I want to select the cap:description where the cap:area/cap:areaDesc = "Choctaw (Alabama)"

    Here's the Xml doc

    Thanks in advance.

    PS I can get the cap:info node by using //cap:info[cap:area/cap:areaDesc='Choctaw (Alabama)'] but I only want the cap:description .
    Attached Files
    -Rupp
    sigpic

    #2
    Rupp,

    There might be a better way but here is one way to do it. First you select a list of nodes matching your critera (the part you already posted) and then you iterate through each of the nodes in the list and extract the data you want. If you put your xml file in the \html directory and run the code I posted you will see two descriptions matching your request.

    Hope this helps.

    <%
    Option Explicit

    Dim strPath, xmlfile, oNodeList, oNode, x

    strPath = hs.GetAppPath & "\html\al.xml"

    Set xmlfile = Server.CreateObject("Microsoft.XMLDOM")
    xmlfile.async=false
    xmlfile.load(strpath)
    xmlfile.setProperty "SelectionLanguage", "XPath"
    xmlfile.setProperty "SelectionNamespaces", "xmlns:cap='http://www.incident.com/cap/1.0'"

    If xmlfile.parseError.errorcode = 0 Then
    Set oNodeList = xmlfile.selectNodes("//cap:info[cap:area/cap:areaDesc='Choctaw (Alabama)']")
    For x = 1 to oNodeList.Length
    Set oNode = oNodeList.nextNode
    'test to make sure node exists in case xml not well formed
    If NodeExists(oNode,"cap:description") Then
    response.write oNode.selectSingleNode("cap:description").Text
    response.write "<hr>"
    End If
    Next

    set oNode=Nothing
    set oNodeList=Nothing
    set xmlfile=Nothing

    End If

    '*****************************************
    Function NodeExists(oInNode, strSelect)

    Dim oNodeTest

    Set oNodeTest = oInNode.SelectSingleNode(strSelect)

    If Not oNodeTest Is Nothing Then
    NodeExists=vbTrue
    Else
    NodeExists=vbFalse
    End If

    Set oNodeTest = Nothing

    End Function

    %>

    Comment


      #3
      Thanks a million Joe. This is exactly what I was looking for. I'm just getting started with this XPath syntax and it's a struggle finding people who know the syntax well enough to help. Thanks again for the excellent example. Greg
      -Rupp
      sigpic

      Comment


        #4
        Anytime Rupp. Glad I could help. The example I posted can be streamlined just a tad by using FOR EACH to step through the nodes instead of the manual FOR loop I posted.

        Replace these two lines:
        For x = 1 to oNodeList.Length
        Set oNode = oNodeList.nextNode

        With this one line:
        For Each oNode in oNodeList

        If you haven't already found these, here are a couple of links you might find useful regarding XPath:

        XPath Tutorial

        XPath Syntax

        And here is the XML SDK (it's a help file with a bunch of good info)

        Comment

        Working...
        X