Xpath syntax to select nodes with multiple attributes in the path

前端 未结 1 1408
逝去的感伤
逝去的感伤 2021-01-07 13:50

My xml tree contains this in the structure


    data1
    da         


        
相关标签:
1条回答
  • 2021-01-07 14:27

    EDITED

    Using your updated XML, this code gives me "data2" and "data2" as output:

    Sub Tester3()
    
        Dim xmlDoc As New MSXML2.DOMDocument30
        Dim objNodes As IXMLDOMNodeList, o As Object
    
        xmlDoc.async = False
        xmlDoc.LoadXML Range("C1").Value
        xmlDoc.setProperty "SelectionLanguage", "XPath"
    
        '### this takes care of the namespace ###
        xmlDoc.setProperty "SelectionNamespaces", _
                         "xmlns:xx='uri:mybikes:wheels'"
    
        If xmlDoc.parseError.errorCode <> 0 Then
    
            MsgBox "Error!" & vbCrLf & _
            "  Line: " & xmlDoc.parseError.Line & vbCrLf & _
            "  Text:" & xmlDoc.parseError.srcText & vbCrLf & _
            "  Reason: " & xmlDoc.parseError.reason
    
        Else
            '### note: adding the namespace alias prefix defined above ###
            Set objNodes = xmlDoc.SelectNodes("//xx:foo[@name='bar']/xx:location[@order='2']")
    
            If objNodes.Length = 0 Then
                Debug.Print "not found"
            Else
                For Each o In objNodes
                    Debug.Print o.nodeTypedValue
                Next o
            End If 'have line items
    
        End If 'parsed OK
    End Sub
    

    Similar Q previously: How to ignore a XML namespace

    0 讨论(0)
提交回复
热议问题