2017-07-30 81 views
0

我的XML樹包含此結構中的XPath語法在路徑選擇具有多個屬性節點

<foo name="bar"> 
    <location order="1">data1</location> 
    <location order="2">data2</location> 
</foo> 

我想是指任何地方的樹,其中位序=「2」(有可能是其中的1000)來操縱所有data2文本。我無法讓vba通過xpath字符串分配來識別它。

我嘗試過很多事情,但,使事情是很有道理的,在我看來

xPath = "//prefix:foo[@name='bar']/location[@order='2']" 

但它確實承認XPATH分配給foo如果我刪除。

"/location[@order='2']" 

我在xpath語法中做錯了什麼?還是有更多的分配一個路徑到一個包含多個屬性的節點來在樹結構中選擇?

也許我想使用錯誤的方法來訪問變量?

Dim list as IXMLDOMNodeList 
Set list = xDoc.SelectNodes(xPath) 
Debug.Print list.length 

給了我一個0,但也有一些特定節點的兩個實例在我的XML

編輯: 仍然給了我零做一些事情與你的後。這裏有一個例子xml,所以你可以看到命名空間。如果我忽略「/ location [@ order ='2']」,我仍然可以打印出一個長度。另外要說明的是,我只對這條道路感興趣,即可能有許多其他foo節點與孩子在一起。這些我現在不在乎。

<?xml version="1.0" encoding="utf-8"?> 
    <IPSGDatas xsi:schemaLocation="uri:mybikes:wheels MYBIKES%20WHEELS%202012.xsd" 
     xmlns="uri:mybikes:wheels" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
     <header> 
      <language_id>120</language_id> 
     </header> 
     <datas> 
      <good> 
       <signature/> 
       <bike> 
        <foos> 
         <marker> 
          <location order="1">data1</location> 
          <location order="2">data2</location> 
         </marker> 
         <foo name="bar"> 
          <location order="1">data1</location> 
          <location order="2">data2</location> 
         </foo> 
        </foos> 
        <profile_id>MyName1</profile_id> 
       </bike> 
       <action_id>New</action_id> 
       <index_id>1</index_id> 
       <agency/> 
       <agency_reference/> 
       <accreditation_id>U</accreditation_id> 
      </good> 
      <good> 
       <signature/> 
       <bike> 
        <foos> 
         <marker> 
          <location order="1">data1</location> 
          <location order="2">data2</location> 
         </marker> 
         <foo name="bar"> 
          <location order="1">data1</location> 
          <location order="2">data2</location> 
         </foo> 
        </foos> 
        <profile_id>MyName2</profile_id> 
       </bike> 
       <action_id>New</action_id> 
       <index_id>1</index_id> 
       <agency/> 
       <agency_reference/> 
       <accreditation_id>U</accreditation_id> 
      </good> 
     </datas> 
    </IPSGDatas> 
+0

'」 foo [@ name ='bar']/location [@ order ='2']「'爲我工作 - 但您似乎沒有在您的示例XML中包含某些名稱空間,因此很難確定。你可以讓它更具代表性的實際使用情況? –

+0

嗯。從我的手機上打字並在無法訪問的情況下使用筆記本電腦上的文件。給我一點,直到我做,我會看看我能做些什麼。目前,我可以告訴你,沒有位置順序=「2」節點,名稱空間工作得很好。 – Dan

+0

:/也許我使用了錯誤的方法。 – Dan

回答

1

EDITED

使用更新後的XML,這個代碼給我 「數據2」 和 「數據2」 作爲輸出:

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 

類似Q前面:How to ignore a XML namespace

+0

非常感謝。我會盡力與此合作。 – Dan

+0

謝謝Tim的幫助!我沒有將他的命名空間前綴設置爲「xx」,儘管我在xPath字符串中的第二個屬性之前注意到了包含前綴的位置......所以我也這樣做了,瞧!這正是我所尋找的,並且工作得很好!再次感謝! – Dan

+0

很高興聽到你有它的工作 –

相關問題